Extract numbers from string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • zek2005

    Extract numbers from string

    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

    1820
    1823
    1825

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

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

    Can anyone help me???

    Thank you very much,

    Ezequiel

  • Charlie King

    #2
    Re: Extract numbers from string

    On 14 Jan 2006 12:39:27 -0800, in
    <1137271167.529 986.168260@g14g 2000cwa.googleg roups.com>
    (comp.lang.php) "zek2005" <esapoznik@gmai l.com> wrote:
    [color=blue]
    > 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
    >
    > 1820
    > 1823
    > 1825
    >
    > Example 2: 1 5 21 31 <<> must be transform into
    > 1
    > 5
    > 21
    > 31
    >
    > The difference between ex 1 and 2 is that there might be different
    > length between numbers.
    >
    > Can anyone help me???[/color]

    $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.

    Cheers

    --
    Charlie

    Comment

    • Oli Filth

      #3
      Re: Extract numbers from string

      zek2005 said the following on 14/01/2006 20:39:[color=blue]
      >
      > I have a varchar field in my DB with numeric values separates by
      > spaces.[/color]

      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.

      [color=blue]
      > I need to extract the numbers to create an array.[/color]

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


      --
      Oli

      Comment

      • john.d.mann@sbcglobal.net

        #4
        Re: Extract numbers from string

        Charlie King wrote:[color=blue]
        > (comp.lang.php) "zek2005" <esapoznik@gmai l.com> wrote:[color=green]
        >>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
        >>1820
        >>1823
        >>1825[/color]
        >
        > $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.
        >
        > Cheers
        >[/color]

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

        Comment

        Working...