Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #3855

preg_match() oddities and question

From Sandman <mr@sandman.net>
Newsgroups comp.lang.php
Subject preg_match() oddities and question
Date 2011-11-22 12:21 +0100
Message-ID <mr-5B96D1.12212022112011@News.Individual.NET> (permalink)

Show all headers | View raw


So I have this regexp:

if (preg_match("/^(.*?)\s*(\d*?)\s*([A-Z,a-z,-]*?)$/", $search, $m)){
    $streetname = uc_words($m[1]);
    $streetnumber = trim($m[2]);
    $streetletter = strtoupper($m[3]);
    $search = trim($streetname . SPACE . $streetnumber . 
$streetletter);
}

The desired result is taki9ng the input ($search) and split it into 
its parts as an address, right? $search can be, for example, "foo 
street 34", "longstreet 45b", "longstreet 45 b" or just "longstreet".

So, if I print_r($m) with different input I get:

Array
(
    [0] => foo street 34
    [1] => foo street
    [2] => 34
    [3] => 
)
Array
(
    [0] => longstreet 45b
    [1] => longstreet
    [2] => 45
    [3] => b
)
Array
(
    [0] => longstreet 45 b
    [1] => longstreet
    [2] => 45
    [3] => b
)

You get the idea. But problems arise when I search for the streetname 
alone:

Array
(
    [0] => longstreet
    [1] => 
    [2] => 
    [3] => longstreet
)

As you can see, the last group "([A-Z,a-z,-]*?)" matches the entire 
search term since there are no digits and the first group is 
non-greedy. And if I make the first group greedy, "longstreet" is 
matched correctly, but it also catches the entire "longstreet 45b" 
when searching for that.

Also, when searching for a term in swedish characters, I get this:

Array
(
    [0] => vikavägen
    [1] => vikavä
    [2] => 
    [3] => gen
)

Which is quite odd to me, why isn't "vikavägen" matched the same 
(undesired) way that "oongstreet". I have tried the /u modifier, and 
made sure that it was utf8-encoded, but it didn't make a difference 
(incoming encoding is ISO 8859-1).

Why the difference, and how do I correctly parse out parts as needed?

Any help is appreciated. 





-- 
Sandman[.net]

Back to comp.lang.php | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

preg_match() oddities and question Sandman <mr@sandman.net> - 2011-11-22 12:21 +0100
  Re: preg_match() oddities and question The Natural Philosopher <tnp@invalid.invalid> - 2011-11-22 11:26 +0000
    Re: preg_match() oddities and question Sandman <mr@sandman.net> - 2011-11-22 12:36 +0100
    Re: preg_match() oddities and question Jerry Stuckle <jstucklex@attglobal.net> - 2011-11-22 07:22 -0500
  Re: preg_match() oddities and question tony@mountifield.org (Tony Mountifield) - 2011-11-22 11:47 +0000
    Re: preg_match() oddities and question Sandman <mr@sandman.net> - 2011-11-22 13:12 +0100
  Re: preg_match() oddities and question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-22 13:30 +0100
    Re: preg_match() oddities and question Sandman <mr@sandman.net> - 2011-11-22 13:55 +0100
      Re: preg_match() oddities and question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-22 17:56 +0100
        Re: preg_match() oddities and question The Natural Philosopher <tnp@invalid.invalid> - 2011-11-22 17:30 +0000
          Re: preg_match() oddities and question "Peter H. Coffin" <hellsop@ninehells.com> - 2011-11-22 17:20 -0600
            Re: preg_match() oddities and question The Natural Philosopher <tnp@invalid.invalid> - 2011-11-22 23:59 +0000
            Re: preg_match() oddities and question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-23 01:59 +0100
              Re: preg_match() oddities and question Sandman <mr@sandman.net> - 2011-11-23 09:58 +0100
                Re: preg_match() oddities and question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-23 22:02 +0100
                Re: preg_match() oddities and question Sandman <mr@sandman.net> - 2011-11-24 08:20 +0100
                Re: preg_match() oddities and question Denis McMahon <denismfmcmahon@gmail.com> - 2011-11-24 12:55 +0000
                Re: preg_match() oddities and question Sandman <mr@sandman.net> - 2011-11-25 09:36 +0100
                Re: preg_match() oddities and question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-24 22:41 +0100
                Re: preg_match() oddities and question Sandman <mr@sandman.net> - 2011-11-25 09:26 +0100
                Re: preg_match() oddities and question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-25 15:44 +0100
                Re: preg_match() oddities and question Sandman <mr@sandman.net> - 2011-11-25 16:34 +0100
                Re: preg_match() oddities and question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-25 23:23 +0100
              Re: preg_match() oddities and question The Natural Philosopher <tnp@invalid.invalid> - 2011-11-23 09:35 +0000
        Re: preg_match() oddities and question Sandman <mr@sandman.net> - 2011-11-23 09:55 +0100
          Re: preg_match() oddities and question "Peter H. Coffin" <hellsop@ninehells.com> - 2011-11-23 07:53 -0600
            Re: preg_match() oddities and question Sandman <mr@sandman.net> - 2011-11-23 19:01 +0100
              Re: preg_match() oddities and question The Natural Philosopher <tnp@invalid.invalid> - 2011-11-23 18:54 +0000
                Re: preg_match() oddities and question Sandman <mr@sandman.net> - 2011-11-23 20:23 +0100
              Re: preg_match() oddities and question "Peter H. Coffin" <hellsop@ninehells.com> - 2011-11-23 12:58 -0600
                Re: preg_match() oddities and question Sandman <mr@sandman.net> - 2011-11-24 08:28 +0100
  SOLVED: Re: preg_match() oddities and question Sandman <mr@sandman.net> - 2011-11-25 10:32 +0100
    Re: SOLVED: Re: preg_match() oddities and question Jerry Stuckle <jstucklex@attglobal.net> - 2011-11-25 18:55 -0500
      Re: SOLVED: Re: preg_match() oddities and question Sandman <mr@sandman.net> - 2011-11-26 11:21 +0100

csiph-web