Roych Posted November 3, 2021 Posted November 3, 2021 Hello I'm trying to remove first four characters from a title field (or custom text field), how would I do this? I have titles like (they are imported with csv and numbers are for automatic categorising sort so they have to be there) 01 - Category1 02 - Category2 03 - Category3 I only want to output the Category1, category2, etc... Any help appreciated Thank you R
horst Posted November 3, 2021 Posted November 3, 2021 The PHP function substr() can be used to start after the 4. character. You can use it in your template file or create a little textformatter module. (Sorry im on mobile, no link to the PHP manual provided.) 2
kongondo Posted November 3, 2021 Posted November 3, 2021 (edited) @horstbeat me to it ?. Two options: Option 1: substr() https://www.php.net/releases/8.0/en.php (+ trim() only if needed) edit. <?php namespace ProcessWire; $originalString = '01 - Category1'; // will remove the space after 01 - for you as well $str = substr($originalString, 5); // debug // d($str,'substr'); Option 2: array explode() + trim() <?php namespace ProcessWire; $originalString = '01 - Category1'; // explode the string on the hyphen ('-') // this creates an array $str2Array = explode("-",$originalString); // our string is in the second position // we get it and remove spaces on each side $str2 = trim($str2Array[1]); // debug // d($str2Array,'$str2Array'); // d($str2,'str2'); Edited November 4, 2021 by kongondo typos 2
Roych Posted November 3, 2021 Author Posted November 3, 2021 Uff, that simple ? I was overthinking everything. ? Thank you very much ? R
Nicolas Posted November 3, 2021 Posted November 3, 2021 Hi, At what time do you want to remove thosecharacters: when importing the data, when displaying the title on the frontend ? depending on your need this could be achieed with a substr PHP function (when displaying the title on the frontend) or via a hook when importing/saving the title.
kongondo Posted November 4, 2021 Posted November 4, 2021 7 hours ago, Roych said: Uff, that simple I fixed some typos in my code, by the way, in case you didn't see them ?. 1
Roych Posted November 4, 2021 Author Posted November 4, 2021 4 hours ago, kongondo said: I fixed some typos in my code, by the way, in case you didn't see them ?. I only used "substr($title, 5)" string and works just fine. ? Thank you R 1
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now