Reply to Re: [PHP] Optimal Regex?

Your name:

Reply:


Posted by Jochem Maas on 09/26/05 22:32

David Pollack wrote:
> I need to catch all instances of a coordinate in a give string. So far I
> have this, which only works if the coordinate is in a certain format.
>
>
> $string = (334,-53.44),(111,222);
> $expression = '([(][-]?[0-9]*[.0-9]*?,[-]?[0-9]*[.0-9]*?[)])';

here's a horrid little bit of .... for you :)

<?php

$testStr = <<< EOD
> \$string = (334,-53.44),(111,222);
> preg_match_all(\$expression, "\$string", \$matches);
>
> This returns the correct strings (334,-53.44) and (111,222), but Id like
> all formats to work. Like (111, 222), ( 111 , 222 ), (111, 222 ), etc, etc.
> How would I change my regex to accomplish this?
EOD;

$coordRE = "(?:[ ]?[-+]?\\d+(?:\\.\\d+)?[ ]?)";
$coordsRE = "#(\\({$coordRE},{$coordRE}\\))#";
$matches = array();
preg_match_all($coordsRE, $testStr, $matches);
var_dump($testStr, $matches);


?>

> preg_match_all($expression, "$string", $matches);
^-- why ? ...

are you wrapping the variable in double quotes?

>
> This returns the correct strings (334,-53.44) and (111,222), but I'd like
> all formats to work. Like (111, 222), ( 111 , 222 ), (111, 222 ), etc, etc.
> How would I change my regex to accomplish this?
>
> --
> David Pollack
> DHPollack@gmail.com
>
> www.atlspecials.com <http://www.atlspecials.com>
> www.da3.net <http://www.da3.net>
>

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация