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! Link to comment https://forums.phpfreaks.com/topic/73609-solved-search-a-string-of-text/ 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"; } ?> Link to comment https://forums.phpfreaks.com/topic/73609-solved-search-a-string-of-text/#findComment-371380 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! Link to comment https://forums.phpfreaks.com/topic/73609-solved-search-a-string-of-text/#findComment-371621 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"; } Link to comment https://forums.phpfreaks.com/topic/73609-solved-search-a-string-of-text/#findComment-371627 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'"; } ?> Link to comment https://forums.phpfreaks.com/topic/73609-solved-search-a-string-of-text/#findComment-372075 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! Link to comment https://forums.phpfreaks.com/topic/73609-solved-search-a-string-of-text/#findComment-372166 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.