php - Not having any luck with preg_match -


im trying parse url last 2 / sections. need address (can that) , number(second section {ie:08078612,08248595} unless @ last , no address present {last url list example} {ie: 8274180}) optionally need last 3 digits of second numbers section.{ie 612,595,180}

im not having luck heres code far.

$url_list[] = $url;  if(preg_match("/[0-9]/",substr(strrchr($url, '/'), 1))) {     $url_list_mls[]= substr(strrchr($url, '/'), 2);     $url_list_mls_last3[] = substr(strrchr($url, '/'), -3);  } else {     $url_list_mls[]= substr(strrchr($url, '/'), 1);     $url_list_mls_last3[] = substr(strrchr($url, '/'), -3); }  $url_list_addy[]= substr(strrchr($url, '/'), 1); 

example urls (part of full url endings examples)

/a019/08078612/214-n-crest-avenue /a019/08248595/111-n-elroy-avenue /a019/8274180 

im trying make 3 lists, address (last section) number (second section) , last 3 numbers of second section number.

the original code way complicated. can capture of strings preg_match_all in single line. since street address part seems conditional, i've made in pattern. grouping last 3 {3} in own parenthesis can use them well. wasn't sure if a019 changed, i've included well, show how done.

<?php  $uris = array("/a019/08078612/214-n-crest-avenue", "/a019/08248595/111-n-elroy-avenue", "/a019/8274180",);  $pattern = "!/([a-za-z][0-9]+)/([0-9]+([0-9]{3}))/?([a-za-z0-9.-]+)?/?!";  $x=0; foreach($uris $uri){     preg_match_all($pattern,$uri,$matches);      $address[$x]['scode'] = $matches[1][0];     $address[$x]['stcode'] = $matches[2][0];     $address[$x]['last3'] = $matches[3][0];     if(!empty($matches[4][0])){         $address[$x]['staddr'] = $matches[4][0];     }     $x++; }   print_r($address);  ?> 

$address output

array (     [0] => array         (             [scode] => a019             [stcode] => 08078612             [last3] => 612             [staddr] => 214-n-crest-avenue         )      [1] => array         (             [scode] => a019             [stcode] => 08248595             [last3] => 595             [staddr] => 111-n-elroy-avenue         )      [2] => array         (             [scode] => a019             [stcode] => 8274180             [last3] => 180         )  ) 

Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -