Find the answer to your Linux question:
Results 1 to 7 of 7
There has gotta be an easy way to do this =) In my database i store the street number and name as one field like: addr = 5252 Lincoln Drive ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2004
    Posts
    2

    Cutting up an address string


    There has gotta be an easy way to do this =)
    In my database i store the street number and name as one field
    like:

    addr = 5252 Lincoln Drive

    I want to break up this data and store each section into a new variable, one being the street number and the other the name.
    Where should I start? I just need a push in the right direction. Thank you guys

    *edit*
    SORRY forgot to mention I'm doing this in PHP on a linux box. Thanks!

  2. #2
    Linux Newbie
    Join Date
    Nov 2004
    Posts
    127
    Is everything standardized like that? Is it always 4 numbers then a space followed by the name? Or maybe is it always a block of characters and then a space.

    You need something to separate it with. If it's always numbers then a space and a name you can use the space as a separator. Although if you sometimes have entries like 17 76 Lincoln Drive then that won't work.

  3. #3
    Just Joined!
    Join Date
    Nov 2004
    Posts
    2
    Quote Originally Posted by Coco
    Is everything standardized like that? Is it always 4 numbers then a space followed by the name? Or maybe is it always a block of characters and then a space.

    You need something to separate it with. If it's always numbers then a space and a name you can use the space as a separator. Although if you sometimes have entries like 17 76 Lincoln Drive then that won't work.
    It won't always be 4 numbers, but it will always be numbers, then a space, then the street name.
    So I would count up to the space, cut that off then store that part in the first variable? What would I use for that substr()?

    Thanks =)

  4. #4
    Linux Engineer
    Join Date
    Sep 2003
    Location
    Knoxhell, TN
    Posts
    1,078
    you would want a function that tokenizes strings.... not sure what the php function is, but in C it's strtok() (go figure that it would actually be named for what it does )
    Their code will be beautiful, even if their desks are buried in 3 feet of crap. - esr

  5. #5
    Linux Engineer Giro's Avatar
    Join Date
    Jul 2003
    Location
    England
    Posts
    1,219
    Use explode to split the string into an array using space(\s) as your deliminator,
    Code:
    $myvar = explode(' ', $string_to_split);

  6. #6
    Linux Newbie
    Join Date
    Nov 2004
    Posts
    127
    You gotta be careful though Giro, he only wants to split on the first space not all spaces.

  7. #7
    Linux Engineer
    Join Date
    Nov 2004
    Location
    Montreal, Canada
    Posts
    1,267
    How is your string made...
    I meen

    _ is it from a web page, where all info are on different textbox..
    _ 1 text box that includes all that info...
    _ one select that returns that info like that...


    please give us info
    \"Meditative mind\'s is like a vast ocean... whatever strikes the surface, the bottom stays calm\" - Dalai Lama
    \"Competition ultimatly comes down to one thing... a loser and a winner.\" - Ugo Deschamps

Posting Permissions

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