Teck Posted October 17, 2007 Share Posted October 17, 2007 I would like to search a string of text for "x" if "x" is found then it echo's something, else it echo's something else... Anyways I'm kind of lost as to the best way to search a string of text (for example "1,2,3,4,5,6,7,8,9,10,11,12") and find lets say "7,"... If someone could point me in the right direction that would be awesome! Quote Link to comment Share on other sites More sharing options...
clearstatcache Posted October 17, 2007 Share Posted October 17, 2007 <?php $mystring = '1,2,3,4,5,6,7,8,9,10,11,12'; $findme = '7'; $pos = strpos($mystring, $findme); // Note our use of ===. Simply == would not work as expected // if the '7' was the 0th (first) character. if ($pos === false) { echo "The string '$findme' was not found in the string '$mystring'"; } else { echo "The string '$findme' was found in the string '$mystring'"; echo " and exists at position $pos"; } ?> Quote Link to comment Share on other sites More sharing options...
Teck Posted October 17, 2007 Author Share Posted October 17, 2007 Nice, I guess the "==" was my problem... Meh Thanks! Quote Link to comment Share on other sites More sharing options...
Teck Posted October 17, 2007 Author Share Posted October 17, 2007 <?php $mystring = '1,2,3,4,5,6,7,8,9,10,11,12'; $findme = '7'; $pos = strpos($mystring, $findme); // Note our use of ===. Simply == would not work as expected // if the '7' was the 0th (first) character. if ($pos === false) { echo "The string '$findme' was not found in the string '$mystring'"; } else { echo "The string '$findme' was found in the string '$mystring'"; echo " and exists at position $pos"; } ?> Just a quick question... could i do something like: $mystring = '1,2,3,4,5,6,7,8,9,10,11,12'; $findme = '43'; $findme2 = '61'; $findme3 = '7'; $pos = strpos($mystring, $findme || $mystring, $findme2 || $mystring, $findme3); if ($pos === false) { echo "The string '$findme' was not found in the string '$mystring'"; } else { echo "The string '$findme' was found in the string '$mystring'"; echo " and exists at position $pos"; } Quote Link to comment Share on other sites More sharing options...
clearstatcache Posted October 18, 2007 Share Posted October 18, 2007 if u want to search multiple string from mystring, i suggest u use preg_match() .... <?php $mystring = '1,2,3,4,5,6,7,8,9,10,11,12'; $findme = '7'; $findme1 = '43'; $findme2 = '61'; $pos = strpos($mystring, $findme); if (preg_match("/$findme|$findme1|$findme2/", $mystring)) { echo "Found in the string '$mystring'"; } else { echo "Not found in the string '$mystring'"; } ?> Quote Link to comment Share on other sites More sharing options...
Teck Posted October 18, 2007 Author Share Posted October 18, 2007 preg_match does exactly what i was looking for! thanks! Quote Link to comment 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.