1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Address Regex

Discussion in 'PHP' started by protocol96, Dec 30, 2008.

  1. #1
    I just found a regex which seems to be working for .net but it refuses to give me any results for php.
    Any Idea
    SEMrush
    
    <?php
    
    $str = '12 Main Street';
    $chars = preg_split('/^(.*\d\S*)\s(.*)$/', $str, -1, PREG_SPLIT_OFFSET_CAPTURE);
    print_r($chars);
    
    ?>
    
    PHP:
    Ideal Output:
    
    
    
    12 Main Street
      -> 12
      -> Main Street
    
    15a Perth Road
      -> 15a
      -> Perth Road
    
    Flat 3b Royal Court
      -> Flat 3b
      -> Royal Court 
    
    PHP:
     
    protocol96, Dec 30, 2008 IP
    nabil_kadimi likes this.
    SEMrush
  2. protocol96

    protocol96 Peon

    Messages:
    413
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    no regex expert here??
     
    protocol96, Dec 31, 2008 IP
  3. nabil_kadimi

    nabil_kadimi Well-Known Member

    Messages:
    1,065
    Likes Received:
    69
    Best Answers:
    0
    Trophy Points:
    195
    #3
    I suggest
    $addresses = array ('15a Perth Road', 'Flat 3b Royal Court', '12 Main Street');
    $pattern = '/^(.*\d+\w*)\s+(.*)$/';           // define the regexp
    
    foreach($addresses as $address){
      $address = trim($address); // remove surrounding whitespace chars
      preg_match($pattern,$address,$chars);
      echo "<pre>";
      print_r($chars);
      echo '</pre></hr>';
    }
    Output:
    Array
    (
        [0] => 15a Perth Road
        [1] => 15a
        [2] => Perth Road
    )
    
    Array
    (
        [0] => Flat 3b Royal Court
        [1] => Flat 3b
        [2] => Royal Court
    )
    
    Array
    (
        [0] => 12 Main Street
        [1] => 12
        [2] => Main Street
    )
    
    Code (markup):
     
    nabil_kadimi, Dec 31, 2008 IP
    protocol96 likes this.