Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.