Extract numbers from string

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hi friends!

I have a varchar field in my DB with numeric values separates by
spaces. I need to extract the numbers to create an array.

Example 1: 1820 1823 1825 --> need to be transform into


Example 2: 1 5 21 31 <<> must be transform into

The difference between ex 1 and 2 is that there might be different
length between numbers.

Can anyone help me???

Thank you very much,


Re: Extract numbers from string

On 14 Jan 2006 12:39:27 -0800, in

Quoted text here. Click to load it

$myArray = explode(" ", $myString)

explode() splits up the string in its second argument at each
ocurrence of the string in its first argument, and returns the results
into an array.

The manual http://fr.php.net/manual/en/function.explode.php
gives good examples.



Re: Extract numbers from string

Charlie King wrote:
Quoted text here. Click to load it

Exactly - I was going to say the same thing if no one else had yet :)

Re: Extract numbers from string

zek2005 said the following on 14/01/2006 20:39:
Quoted text here. Click to load it

Well this is generally a bad idea to start with - one of the principles  
of good database design is atomicity, i.e. each field should contain  
only one value.

i.e. instead of something like:

ID      name      values(VARCHAR)
1       Alice     1 28 373
2       Bob       72 182 44

you should use two tables:

ID      name
1       Alice
2       Bob

personID   value(INT)
1          1
1          28
1          373
2          72
2          182
2          44

Not to mention that storing numeric values in a character-based data  
type is a waste of space and processing time.

Quoted text here. Click to load it

But if you must store your info this way, use explode() to extract the  
individual values.


Site Timeline