piyush23424 Posted February 18, 2010 Share Posted February 18, 2010 Hi, do we have any php inbuilt function which returns true or false after checking a particular character (need to check comma) in a given string. actually i want to conditionally explode a string i.e if string is having comma it will explode it by comma otherwise by space. Thanks Link to comment https://forums.phpfreaks.com/topic/192511-general-question-regarding-php-functions/ Share on other sites More sharing options...
flappy_warbucks Posted February 18, 2010 Share Posted February 18, 2010 Hi, do we have any php inbuilt function which returns true or false after checking a particular character (need to check comma) in a given string. actually i want to conditionally explode a string i.e if string is having comma it will explode it by comma otherwise by space. Thanks how about: if (preg_match(","$string)) { $string_array = explode(",",$string); } That should do the trick. Link to comment https://forums.phpfreaks.com/topic/192511-general-question-regarding-php-functions/#findComment-1014301 Share on other sites More sharing options...
jl5501 Posted February 18, 2010 Share Posted February 18, 2010 look at the 2 functions strpos() and strstr() http://www.php.net/manual/en/function.strpos.php http://php.net/manual/en/function.strstr.php Link to comment https://forums.phpfreaks.com/topic/192511-general-question-regarding-php-functions/#findComment-1014302 Share on other sites More sharing options...
piyush23424 Posted February 18, 2010 Author Share Posted February 18, 2010 if (preg_match(",",$searchString)) { $string_array = explode(",", $searchString); } else { $string_array = explode(" ", $searchString); } echo "<pre>"; print_r($string_array); Output is warning error : Severity: Warning Message: preg_match() [function.preg-match]: No ending delimiter ',' found Filename: controllers/cma.php Line Number: 104 but its doing explode with space Array ( [0] => 779,TAMIAMI [1] => TRL [2] => #6 ) Link to comment https://forums.phpfreaks.com/topic/192511-general-question-regarding-php-functions/#findComment-1014314 Share on other sites More sharing options...
jl5501 Posted February 18, 2010 Share Posted February 18, 2010 preg_match() is a bit overkill for just looking for a character in a string if(strpos($string,',')) is less overhead Link to comment https://forums.phpfreaks.com/topic/192511-general-question-regarding-php-functions/#findComment-1014315 Share on other sites More sharing options...
flappy_warbucks Posted February 18, 2010 Share Posted February 18, 2010 if (preg_match(",",$searchString)) { $string_array = explode(",", $searchString); } else { $string_array = explode(" ", $searchString); } echo "<pre>"; print_r($string_array); Output is warning error : Severity: Warning Message: preg_match() [function.preg-match]: No ending delimiter ',' found Filename: controllers/cma.php Line Number: 104 but its doing explode with space Array ( [0] => 779,TAMIAMI [1] => TRL [2] => #6 ) I never tested the code, the following code works for me: $string = "one,2,three,4"; if (preg_match("/,/",$string)) { $string_array = explode(",",$string); } echo "<pre>"; print_r($string_array); Link to comment https://forums.phpfreaks.com/topic/192511-general-question-regarding-php-functions/#findComment-1014323 Share on other sites More sharing options...
piyush23424 Posted February 18, 2010 Author Share Posted February 18, 2010 Thanks for your quick replies . its working fine with strpos() function ... 8).. Link to comment https://forums.phpfreaks.com/topic/192511-general-question-regarding-php-functions/#findComment-1014326 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.