ksduded Posted October 23, 2009 Share Posted October 23, 2009 I want to check for the word CATEGORY: inside a string which can be CATEGORY: ITEM and remove the word CATEGORY: leaving only ITEM inside the string.... In case the string does not have CATEGORY: before listing the ITEM then nothing happens... Thanks Quote Link to comment https://forums.phpfreaks.com/topic/178781-solved-how-to-use-substr/ Share on other sites More sharing options...
Alex Posted October 23, 2009 Share Posted October 23, 2009 If I understand you correctly you'd probably want to use explode() ex: <?php $str = 'CATEGORY: ITEM'; echo next(explode(': ', $str)); // ITEM In case you were wondering: next() is only ultized in my example to grab the second element (index of 1) of the array returned by explode(). Because the default array pointer is 0, so using next() increments that to 1 then returns that element. So it would be no different than doing: <?php $str = 'CATEGORY: ITEM'; $arr = explode(': ', $str); echo $arr[1]; Quote Link to comment https://forums.phpfreaks.com/topic/178781-solved-how-to-use-substr/#findComment-943092 Share on other sites More sharing options...
lemmin Posted October 23, 2009 Share Posted October 23, 2009 You could just preg_replace to just replace the string with nothing: $string = preg_replace("/CATEGORY:/", null, $string); http://us.php.net/manual/en/function.preg-replace.php Quote Link to comment https://forums.phpfreaks.com/topic/178781-solved-how-to-use-substr/#findComment-943096 Share on other sites More sharing options...
ksduded Posted October 23, 2009 Author Share Posted October 23, 2009 $str = 'CATEGORY: ITEM'; echo next(explode(': ', $str)); // ITEM[/code] Thanks, this worked for me to an extent. However, I want to first check whether the string has CATEGORY: in it before exploding it.... if it does not, then it can move ahead.... This is what I am doing $str = $title; $title = next(explode(': ', $str)); $title has the string... it can be either ITEM or CATEGORY: ITEM (and it keeps on changing according to the different web page), so ideally i would first check for CATEGORY: inside the title, and if found then only do the explode function.... how do I go about that. Quote Link to comment https://forums.phpfreaks.com/topic/178781-solved-how-to-use-substr/#findComment-943127 Share on other sites More sharing options...
Alex Posted October 23, 2009 Share Posted October 23, 2009 You can use: echo (strpos($str, 'CATEGORY: ') !== false) ? next(explode(': ', $str)) : $str; Basically what it does is check to see if 'CATEGORY: ' is found inside $str using strpos(). If it does then it explodes and gets ITEM out of it, like in my previous example. If not it just echoes $str Quote Link to comment https://forums.phpfreaks.com/topic/178781-solved-how-to-use-substr/#findComment-943136 Share on other sites More sharing options...
Daniel0 Posted October 23, 2009 Share Posted October 23, 2009 In case you were wondering: next() is only ultized in my example to grab the second element (index of 1) of the array returned by explode(). An alternate way of doing that would be list(, $title) = explode(': ', $str); Quote Link to comment https://forums.phpfreaks.com/topic/178781-solved-how-to-use-substr/#findComment-943137 Share on other sites More sharing options...
ksduded Posted October 23, 2009 Author Share Posted October 23, 2009 You can use: echo (strpos($str, 'CATEGORY: ') !== false) ? next(explode(': ', $str)) : $str; Basically what it does is check to see if 'CATEGORY: ' is found inside $str using strpos(). If it does then it explodes and gets ITEM out of it, like in my previous example. If not it just echoes $str Ok, I am being a complete noob here, but I don't want the echo out the result. Just store it in the $str variable. Otherwise it is working out perfectly. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/178781-solved-how-to-use-substr/#findComment-943141 Share on other sites More sharing options...
Alex Posted October 23, 2009 Share Posted October 23, 2009 Then just change echo to $str = $str = (strpos($str, 'CATEGORY: ') !== false) ? next(explode(': ', $str)) : $str; Quote Link to comment https://forums.phpfreaks.com/topic/178781-solved-how-to-use-substr/#findComment-943149 Share on other sites More sharing options...
salathe Posted October 23, 2009 Share Posted October 23, 2009 Here are three more variations on the theme. They all take the variable $str and chomp off the "CATEGORY: " prefix if there is one. The first two require the prefix to be at the start of the string. $str = "CATEGORY: ITEM"; // If prefix exists for string, remove it. $prefix = "CATEGORY: "; if (strpos($str, $prefix) === 0) { $out = substr($str, strlen($prefix)); } // Same as above $out = preg_replace("/^CATEGORY: /", "", $str); // (Almost) same as above $out = str_replace("CATEGORY: ", "", $str); Quote Link to comment https://forums.phpfreaks.com/topic/178781-solved-how-to-use-substr/#findComment-943160 Share on other sites More sharing options...
ksduded Posted October 23, 2009 Author Share Posted October 23, 2009 Then just change echo to $str = $str = (strpos($str, 'CATEGORY: ') !== false) ? next(explode(': ', $str)) : $str; VOILA!!! ITs working ... thanks Quote Link to comment https://forums.phpfreaks.com/topic/178781-solved-how-to-use-substr/#findComment-943162 Share on other sites More sharing options...
ksduded Posted October 23, 2009 Author Share Posted October 23, 2009 wow you guys are great... thanks for all the suggestions..... I was banging my head on the wall before I came here.. Quote Link to comment https://forums.phpfreaks.com/topic/178781-solved-how-to-use-substr/#findComment-943168 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.