SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How do I Split an alphanumeric string into array?

    Hi,

    I have the following string = "Mr Brown 40 Jones 21 Smith 18"

    How do I split the string into Name and Numbers , like this

    $person[0]="Mr Brown"
    $numbers[0]="40"
    $person[1]="Jones"
    $numbers[1]="21"
    $person[2]="Smith"
    $numbers[2]="18"

    Thanks

  2. #2
    SitePoint Addict
    Join Date
    Apr 2011
    Posts
    265
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    Try use this code:
    PHP Code:
    $string "Mr Brown 40 Jones 21 Smith 18";
    $person $numbers = array();

    if(
    preg_match_all('/([a-z ]+[0-9]+)/i'$string$mt)) {
      
    $nrmt count($mt[0]);
      for(
    $i=0$i<$nrmt$i++) {
        if(
    preg_match('/([a-z ]+)([0-9]+)/i'$mt[0][$i], $mt2)) {
          
    $person[$i] = trim($mt2[1]);
          
    $numbers[$i] = $mt2[2];
        }
      }
    }
    else echo 
    'Not matche';

    // test

    echo '<pre>';
    var_export($person);
    var_export($numbers);
    echo 
    '</pre>'
    Free: Web Programming Courses HTML, CSS, Flash
    Web Programming: AJAX Course and PHP-MySQL Course video Lessons
    Good JavaScript and jQuery course for beginners

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by MarPlo View Post
    Hi,
    Try use this code:
    PHP Code:
    $string "Mr Brown 40 Jones 21 Smith 18";
    $person $numbers = array();

    if(
    preg_match_all('/([a-z ]+[0-9]+)/i'$string$mt)) {
      
    $nrmt count($mt[0]);
      for(
    $i=0$i<$nrmt$i++) {
        if(
    preg_match('/([a-z ]+)([0-9]+)/i'$mt[0][$i], $mt2)) {
          
    $person[$i] = trim($mt2[1]);
          
    $numbers[$i] = $mt2[2];
        }
      }
    }
    else echo 
    'Not matche';

    // test

    echo '<pre>';
    var_export($person);
    var_export($numbers);
    echo 
    '</pre>'
    wow, thanks for the quick response... it seems to work perfectly..

  4. #4
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just going back to my first post, I have another problem I'm not sure if its possible to work around - the above code works great with string like this "Mr Brown 40 Jones 21 Smith 18" but sometimes the string dos not have numbers in it, for example string = "Mr Brown Jones 21" this causes the above code to stop working.

    Would it be possible to have a failsafe which adds the value "na" to the $numbers[i] string if number are missing from the string?

    Thanks

  5. #5
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    72 Post(s)
    Tagged
    0 Thread(s)
    How does the script know where numbers belong?

    "Mr Brown Jones 21 Simon 48"

    Should it put out
    Mr na Brown Jones 21 Simon 48?
    What about
    Mr Brown na Jone 21 Simon 48
    or
    Mr na Brown na Jones 21 Simon 48?
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  6. #6
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    How does the script know where numbers belong?

    "Mr Brown Jones 21 Simon 48"

    Should it put out
    Mr na Brown Jones 21 Simon 48?
    What about
    Mr Brown na Jone 21 Simon 48
    or
    Mr na Brown na Jones 21 Simon 48?
    I would like itto put out if possible - Mr Brown na Jone 21 Simon 48 if the string is missing numbers

  7. #7
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    72 Post(s)
    Tagged
    0 Thread(s)
    My point is, how can the script know where the numbers are missing?

    "Mr Brown Jones" may be a valid name.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  8. #8
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    It might shed some light if you explained where the string was originally coming from.

  9. #9
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    It might shed some light if you explained where the string was originally coming from.
    They are being pulled a mysql table from a database table, but as the above post mentioned I don't think it may be possible to do what I want

  10. #10
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    So are you really saying the whole string is stored as one single column from the table?

    ie "select mystring from mytable where id = 1"

    Code:
    mytable
    =====
    id | mystring
     1 | "Mr Brown 40 Jones 21 Smith 18"
    If so, then how did it get into your database?

    Where did it originate from?

  11. #11
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    So are you really saying the whole string is stored as one single column from the table?

    ie "select mystring from mytable where id = 1"

    Code:
    mytable
    =====
    id | mystring
     1 | "Mr Brown 40 Jones 21 Smith 18"
    If so, then how did it get into your database?

    Where did it originate from?
    Yes correct, all in one string

    It the data was originally pulled in from a spreadsheet

  12. #12
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    So ... did the spreadsheet have all these values in one singe cell?

  13. #13
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,158
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    So ... did the spreadsheet have all these values in one singe cell?
    And if they didn't, why didn't you just export it as a CSV and import it into separate columns?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •