Log in

View Full Version : preg regex end parameter /Ui



djr33
06-19-2010, 02:53 PM
http://www.php.net/manual/en/function.preg-replace.php#77255

That's the example I am using (adapted some).

I don't now what the /Ui means after the regex. I am guessing it's some sort of parameter.

From testing it looks like it makes it "not greedy" so it finds the shortest string possible rather than finding the farthest instances to match. Removing it makes it take a longer string, rather than the shortest possible.

Is this correct?


And is there a list of these parameters somewhere?

I looked around but couldn't find a reference on this specifically.


Thanks...

james438
06-19-2010, 07:19 PM
The U and the i are listed after delimiters. These are known as modifiers in that they modify how PCRE operates to some degree. They can also be listed next to each other as in the example you mentioned.

For example:


<?php
$text = "text--hn-tact-----hn---tact";
$text=preg_replace('/(teXt).*?(tact)/Ui','',$text);
echo "$text";
?>
the result is an empty string.

This will look for the word text or TEXT or TeXt all the way up to the word tact. normally .*? will match everything from "text" to the very next "tact" it comes across, but in this example the quantifier greediness has been inverted, so the ? will now match up to the last tact instead. I usually don't bother with the /U modifier.

/U = inverts the greediness of the quantifiers.
/i = makes it case insensitive.

They are placed after the delimiter and before the ending quote and are case sensitive.

You can read more on modifiers here (http://us.php.net/manual/en/reference.pcre.pattern.modifiers.php).

Pretty much everything else can be found here (http://us.php.net/manual/en/reference.pcre.pattern.syntax.php).

I added the extra info for whoever is curious :)

djr33
06-19-2010, 07:43 PM
Thanks, James! Perfect answer and I'll read up on that now. Regex still looks like hieroglyphics to me, but I'm trying to get used to it. When it works, it's great...