tascam424 Posted February 16, 2014 Share Posted February 16, 2014 Hi i'm trying to edit a WP Custom function but i'm stumped. I'm using the following function currently to remove everything in a string up to and including "," Is it possible to edit it to remove everything AFTER and including "," Or is there a better alternative ? function checkstr($x){ if(strpos($x,',')==FALSE){ return trim(substr(strrchr($x, ' '), 1 )); }else{ return trim(substr($x, strpos($x,',')),','); } } Quote Link to comment https://forums.phpfreaks.com/topic/286237-custom-function/ Share on other sites More sharing options...
Ch0cu3r Posted February 16, 2014 Share Posted February 16, 2014 substr($x, strpos($x,',')) will return what is after the comma, if you want to get the text before the comma use substr($x, 0, strpos($x,',')) Quote Link to comment https://forums.phpfreaks.com/topic/286237-custom-function/#findComment-1469156 Share on other sites More sharing options...
tascam424 Posted February 16, 2014 Author Share Posted February 16, 2014 Thanks for your reply, i haven't been able to test it because i am already using the other function so i get an error Fatal error: Cannot redeclare checkstr() (previously declared in what would be the best way to achieve what i need .. i actually need to be able to call the 2 functions seperately .. here is what i have at the moment, which obviously causes the error. /** * Get Color From Title */ function checkstr($x){ if(strpos($x,',')==FALSE){ return trim(substr(strrchr($x, ' '), 1 )); }else{ return trim(substr($x, strpos($x,',')),','); } } /** * Remove Color From Title */ function checkstr($x){ if(strpos($x,',')==FALSE){ return trim(substr(strrchr($x, ' '), 1 )); }else{ return trim(substr($x, 0, strpos($x,',')),','); } } Quote Link to comment https://forums.phpfreaks.com/topic/286237-custom-function/#findComment-1469163 Share on other sites More sharing options...
tascam424 Posted February 16, 2014 Author Share Posted February 16, 2014 I should probably provide more information, i am using WP All Import to import a product csv. The Product titles appear like so : Crossover Crop Top In Burgundy, Burgundy So my first function allows me to extract "Burgundy" from the end to use as Category/Color My second function should strip ",Burgundy" from the end to leave me Crossover Crop Top In Burgundy to use as Product Title. As these are pulled into different fields i can use the same column name with multiple functions, unfortunately it would appear that i can't declare the same function twice in function.php. Quote Link to comment https://forums.phpfreaks.com/topic/286237-custom-function/#findComment-1469166 Share on other sites More sharing options...
mogosselin Posted February 16, 2014 Share Posted February 16, 2014 I don't know how WP works but... why can't you give your functions 2 different names? Quote Link to comment https://forums.phpfreaks.com/topic/286237-custom-function/#findComment-1469171 Share on other sites More sharing options...
tascam424 Posted February 16, 2014 Author Share Posted February 16, 2014 Well i can do that and that seems like the perfect option but is it as simple as renaming the function like so ? function this_is_my_other_checkstr($x){ if(strpos($x,',')==FALSE){ return trim(substr(strrchr($x, ' '), 1 )); }else{ return trim(substr($x, 0, strpos($x,',')),','); } } Thanks for your input ! Quote Link to comment https://forums.phpfreaks.com/topic/286237-custom-function/#findComment-1469172 Share on other sites More sharing options...
tascam424 Posted February 16, 2014 Author Share Posted February 16, 2014 Apparently it is !! Now i feel a little stupid ! Thank you both for your help ! Quote Link to comment https://forums.phpfreaks.com/topic/286237-custom-function/#findComment-1469173 Share on other sites More sharing options...
Ch0cu3r Posted February 16, 2014 Share Posted February 16, 2014 (edited) i am using WP All Import to import a product csv. Why not use str_getcsv? $string = 'Crossover Crop Top In Burgundy, Burgundy'; list($title, $other) = str_getcsv($string); echo 'Text before comma: ' . $title. '<br />'; echo 'Text after comma: '. $other; Edited February 16, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/286237-custom-function/#findComment-1469175 Share on other sites More sharing options...
Barand Posted February 16, 2014 Share Posted February 16, 2014 or explode? $string = 'Crossover Crop Top In Burgundy, Burgundy'; list($title, $other) = explode(',', $string); echo 'Text before comma: ' . $title. '<br />'; echo 'Text after comma: '. $other; Quote Link to comment https://forums.phpfreaks.com/topic/286237-custom-function/#findComment-1469184 Share on other sites More sharing options...
tascam424 Posted February 18, 2014 Author Share Posted February 18, 2014 Thanks guys, this is now working for me, but i need to add to it do also remove the word "Large" from the start of the string, it is consistantly the first word in the string and it is the same word always. What would be the best way to add it to this function to do both. function other_checkstr($x){ if(strpos($x,'-')==FALSE){ return trim(substr(strrchr($x, ' '), 1 )); }else{ return trim(substr($x,0, strpos($x,'-')),'-'); } } Quote Link to comment https://forums.phpfreaks.com/topic/286237-custom-function/#findComment-1469412 Share on other sites More sharing options...
Ch0cu3r Posted February 18, 2014 Share Posted February 18, 2014 Use str_replace Quote Link to comment https://forums.phpfreaks.com/topic/286237-custom-function/#findComment-1469415 Share on other sites More sharing options...
tascam424 Posted February 18, 2014 Author Share Posted February 18, 2014 I appreciate your response, but i would be really greatfull if you could help me further, i really have no idea how to contain it within my existing function. Quote Link to comment https://forums.phpfreaks.com/topic/286237-custom-function/#findComment-1469422 Share on other sites More sharing options...
tascam424 Posted February 20, 2014 Author Share Posted February 20, 2014 So i somehow need to combine this function other_checkstr($x){ if(strpos($x,'-')==FALSE){ return trim(substr(strrchr($x, ' '), 1 )); }else{ return trim(substr($x,0, strpos($x,'-')),'-'); } } and this str_replace("Large","","$x"); Ive tried multiple combinations, but i can't seem to get it to work. Can anybody help me please ? Thank you ! Quote Link to comment https://forums.phpfreaks.com/topic/286237-custom-function/#findComment-1469721 Share on other sites More sharing options...
Barand Posted February 20, 2014 Share Posted February 20, 2014 (edited) What have you tried? Have you tried reading the manual? http://uk1.php.net/manual/en/function.str-replace.php Edited February 20, 2014 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/286237-custom-function/#findComment-1469730 Share on other sites More sharing options...
tascam424 Posted February 20, 2014 Author Share Posted February 20, 2014 Yeh i have been reading the manual, which is how i got this far. Ive also been looking at other functions to try and understand how they can be combined. I tried this function other_checkstr($x){ str_replace("Large","",$x); if(strpos($x,'-')==FALSE){ return trim(substr(strrchr($x, ' '), 1 )); }else{ return trim(substr($x,0, strpos($x,'-')),'-'); } } The function runs the second part successfully as required, but the str_replace has no effect. Quote Link to comment https://forums.phpfreaks.com/topic/286237-custom-function/#findComment-1469742 Share on other sites More sharing options...
Barand Posted February 20, 2014 Share Posted February 20, 2014 Then you haven't read the manual properly. Str_replace returns a string with values replaced. You are not picking up the returned value. function other_checkstr($x){ $x = str_replace("Large","",$x); if(strpos($x,'-')==FALSE){ return trim(substr(strrchr($x, ' '), 1 )); }else{ return trim(substr($x,0, strpos($x,'-')),'-'); } } Quote Link to comment https://forums.phpfreaks.com/topic/286237-custom-function/#findComment-1469748 Share on other sites More sharing options...
tascam424 Posted February 20, 2014 Author Share Posted February 20, 2014 Ahhhh ok, i totally miss-understood .. I am just trying to learn php, as i'm sure you could tell. Thank you very much for your help, it is greatly appreciated !! Quote Link to comment https://forums.phpfreaks.com/topic/286237-custom-function/#findComment-1469753 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.