Jump to content

str_word_count with numbers


dreamwest

Recommended Posts

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

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";

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;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.