mforan Posted May 3, 2010 Share Posted May 3, 2010 Hi, i need a bit of advice on how to split some text... basically i want to turn the text "1,3,5,6,8,9" into individual numbers on there own. basically i need to enter the numbers into an IF like: if ($number = "1" || "3" || "5" || "6" || "8" || "9") { echo "blah"; } i need the IF statement to corrispond to the numbers in the original text everytime. any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/200506-splitting-some-numbers-from-text/ Share on other sites More sharing options...
ChemicalBliss Posted May 3, 2010 Share Posted May 3, 2010 First you split the string into an array: $string = "1,3,5,6,8,9"; $strArray = explode(",",$string); Then you would loop the array, and echo just once if a match is found; foreach($strArray As $number2){ if($number == $number2){ echo("blah"); break; } } -cb- Quote Link to comment https://forums.phpfreaks.com/topic/200506-splitting-some-numbers-from-text/#findComment-1052170 Share on other sites More sharing options...
Alex Posted May 3, 2010 Share Posted May 3, 2010 Not sure, but I think you meant something like this: $string = "1,3,5,6,8,9"; if(in_array($number, explode(',', $string))) { // ... } Quote Link to comment https://forums.phpfreaks.com/topic/200506-splitting-some-numbers-from-text/#findComment-1052171 Share on other sites More sharing options...
mforan Posted May 3, 2010 Author Share Posted May 3, 2010 ahhhh awesome, i didnt know about "foreach" works, very, very nicely! thanks! Quote Link to comment https://forums.phpfreaks.com/topic/200506-splitting-some-numbers-from-text/#findComment-1052178 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.