Re: [PHP] preg_match_all question

2009-01-16 Thread Robert Cummings
On Fri, 2009-01-16 at 09:42 +, Phil Ewington - iModel Ltd. wrote:
 Hi All,
 
 Having an issue with regular expressions, never been my strong point!
 
 The following pattern only picks up one instance per line, if more than 
 one instance exists all text from first {{ to last }} is included, can 
 anyone point out where I am going wrong?
 
 preg_match_all(/\{\{lang:(.*)\}\}/, $str, $tags);

You need the ungreedy modifier:

preg_match_all(/\{\{lang:(.*)\}\}/U, $str, $tags);

Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_match_all question

2009-01-16 Thread Phil Ewington - iModel Ltd.

Robert Cummings wrote:

On Fri, 2009-01-16 at 09:42 +, Phil Ewington - iModel Ltd. wrote:
  

Hi All,

Having an issue with regular expressions, never been my strong point!

The following pattern only picks up one instance per line, if more than 
one instance exists all text from first {{ to last }} is included, can 
anyone point out where I am going wrong?


preg_match_all(/\{\{lang:(.*)\}\}/, $str, $tags);



You need the ungreedy modifier:

preg_match_all(/\{\{lang:(.*)\}\}/U, $str, $tags);

Cheers,
Rob.
  

Rob you're a star, thanks mate.

- Phil

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] preg_match_all question

2009-01-16 Thread Boyd, Todd M.
 -Original Message-
 From: Robert Cummings [mailto:rob...@interjinn.com]
 Sent: Friday, January 16, 2009 4:31 AM
 To: Phil Ewington - iModel Ltd.
 Cc: php-general@lists.php.net
 Subject: Re: [PHP] preg_match_all question
 
 On Fri, 2009-01-16 at 09:42 +, Phil Ewington - iModel Ltd. wrote:
  Hi All,
 
  Having an issue with regular expressions, never been my strong
point!
 
  The following pattern only picks up one instance per line, if more
 than
  one instance exists all text from first {{ to last }} is included,
 can
  anyone point out where I am going wrong?
 
  preg_match_all(/\{\{lang:(.*)\}\}/, $str, $tags);
 
 You need the ungreedy modifier:
 
 preg_match_all(/\{\{lang:(.*)\}\}/U, $str, $tags);

FWIW, you can tell just the .* to be un-greedy using a ? like so:

preg_match_all('/\{\{lang:(.*?)\|\|/', $str, $tags);


// Todd

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] preg_match_all question

2009-01-16 Thread Boyd, Todd M.
 -Original Message-
 From: Boyd, Todd M. [mailto:tmbo...@ccis.edu]
 Sent: Friday, January 16, 2009 2:13 PM
 To: php-general@lists.php.net
 Subject: RE: [PHP] preg_match_all question
 
  -Original Message-
  From: Robert Cummings [mailto:rob...@interjinn.com]
  Sent: Friday, January 16, 2009 4:31 AM
  To: Phil Ewington - iModel Ltd.
  Cc: php-general@lists.php.net
  Subject: Re: [PHP] preg_match_all question
 
  On Fri, 2009-01-16 at 09:42 +, Phil Ewington - iModel Ltd.
wrote:
   Hi All,
  
   Having an issue with regular expressions, never been my strong
 point!
  
   The following pattern only picks up one instance per line, if more
  than
   one instance exists all text from first {{ to last }} is included,
  can
   anyone point out where I am going wrong?
  
   preg_match_all(/\{\{lang:(.*)\}\}/, $str, $tags);
 
  You need the ungreedy modifier:
 
  preg_match_all(/\{\{lang:(.*)\}\}/U, $str, $tags);
 
 FWIW, you can tell just the .* to be un-greedy using a ? like so:
 
 preg_match_all('/\{\{lang:(.*?)\|\|/', $str, $tags);

Correction:

preg_match_all('/\{\{lang:(.*?)\}\}/', $str, $tags);

...turned my squiggly brackets into pipe chars in a fit of randomness.


// Todd

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php