Jump to content

[SOLVED] Preg Syntax Question


Grego

Recommended Posts

Preg Syntax confuses me - I'm just starting to learn it.

 

Anyhow, I have this search term:

"/\{INCLUDE\.*)\}/is"

 

I want it to find {INCLUDE:sometext} on my page. However, my page has two of these statements on it:

{INCLUDE:sometext}
Bit in the middle
{INCLUDE:othertext}

So the preg_match is pulling out the following as a solution:

sometext}
Bit in the middle
{INCLUDE:othertext

 

How can I modify the syntax of "/\{INCLUDE\:(.*)\}/is" to make it only select the first following term, or only a solution before the end of a new line.

 

If I haven't explained that well, please ask questions to determine the problem better. Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/82029-solved-preg-syntax-question/
Share on other sites

Quantifiers are greedy by default. This means they take as much as possible and only backtrack to satisfy later matches.

 

\{INCLUDE\: matches literally.

(.*) gobbles up everything to the end of the string, then backtracks in order to match...

\}, thus matching the last } in the string.

 

Make the quantifier lazy, (.*?), or, even better, use ([^}]*).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.