dreamwest Posted May 31, 2009 Share Posted May 31, 2009 Is there anyway i can split numbers, this code below works with words but i need to seperate a sting of numbers: $string = "Im a string"; foreach(str_word_count($string,1) as $word) echo $word."<br>"; I need to do this with a string of numbers: $string = 12 34 565 32 123; foreach(str_word_count($string,1) as $word) echo $word."<br>"; Link to comment https://forums.phpfreaks.com/topic/160342-str_word_count-with-numbers/ Share on other sites More sharing options...
MadTechie Posted May 31, 2009 Share Posted May 31, 2009 try this $result = explode(' ', $string); if you have a few spaces then this maybe better $result = preg_split('/\s+/', $string); Link to comment https://forums.phpfreaks.com/topic/160342-str_word_count-with-numbers/#findComment-846132 Share on other sites More sharing options...
dreamwest Posted May 31, 2009 Author Share Posted May 31, 2009 Im trying to split it up so i can add extra stuff to each number then make a query $string = 12 34 565 32 123; foreach(str_word_count($string,1) as $id) $id_seperate = "id=".$id." AND"; $query = "SELECT * FROM table WHERE ".$id_seperate." approve=1"; So the query should look like: $query = "SELECT * FROM table WHERE id=12 AND id=34 AND id=565 AND id=32 AND id=123 AND approve=1"; Link to comment https://forums.phpfreaks.com/topic/160342-str_word_count-with-numbers/#findComment-846134 Share on other sites More sharing options...
MadTechie Posted May 31, 2009 Share Posted May 31, 2009 Humm, i think using IN is quicker than using lots of ANDs in anycase, heres a basic conversion <?php #option 1 $string = "12 34 565 32 123"; #$result = preg_split('/\s+/', $string); $result = explode(' ', $string); $result = "id=".implode(" AND id=",$result)." AND approve=1"; echo $result; echo "<br>\n"; #option 2 $string = "12 34 565 32 123"; $string = str_replace(" ",",",$string); $result = "id IN ($string) AND approve=1"; echo $result; ?> Link to comment https://forums.phpfreaks.com/topic/160342-str_word_count-with-numbers/#findComment-846137 Share on other sites More sharing options...
dreamwest Posted May 31, 2009 Author Share Posted May 31, 2009 Humm, i think using IN is quicker than using lots of ANDs in anycase, heres a basic conversion Your right , i looked into it, theres a lot less work too Thanks for the example...works sweetly Link to comment https://forums.phpfreaks.com/topic/160342-str_word_count-with-numbers/#findComment-846156 Share on other sites More sharing options...
MadTechie Posted May 31, 2009 Share Posted May 31, 2009 Topic Solved ? Link to comment https://forums.phpfreaks.com/topic/160342-str_word_count-with-numbers/#findComment-846356 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.