Jump to content

Archived

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

Monkuar

help preg_retch

Recommended Posts

$string = "[img]";
$paragraph = $text;
if (preg_match_all($string, $paragraph, $matches)) {
  echo count($matches[0]) . " matches found";
  exit;
}else {
  echo "match NOT found";
  exit;
}

 

okay, if I use it does not find a match, how do I make it so it's not case sensitive?

Share this post


Link to post
Share on other sites

preg_match does much more than just look for a simple string. It requires you to actually learn how to use the function rather than just throw stuff at it.

if (preg_match_all("/" . preg_quote($string) . "/i", $paragraph, $matches)) {

Share this post


Link to post
Share on other sites

preg_match does much more than just look for a simple string. It requires you to actually learn how to use the function rather than just throw stuff at it.

if (preg_match_all("/" . preg_quote($string) . "/i", $paragraph, $matches)) {

 

doesn't /i mean case sensitive? why does preg_quote need to be used? (just curious) im trying to learn

Share this post


Link to post
Share on other sites

Good, because I didn't want to try explaining things if you didn't care about it (no offense, I'm just lazy).

 

You're looking for the exact string "" (case-insensitive). Regular expressions are overkill for that, but you need a count of matches and there is no built-in PHP function that can do that*.

So now you have to create a regular expression meaning "literally ''". Two things: expressions need delimiters and [ and ] are special characters. What you had before "worked" because preg_match() accepts a [] pair as delimiters, but that meant you were actually only searching for "img". If you had the string

To use img tags use "[img]".

there would have been two matches.

 

So the slashes in the expression are the new delimiters, preg_quote() turns an arbitrary string into something safe for regular expressions (turning the [ and ] into \[ and \]), and the /i flag means case-insensitive.

 

Now that you're in the Regex forum, take a look at the stickies we have here.

 

* substr_count() is case-sensitive.

Share this post


Link to post
Share on other sites

Good, because I didn't want to try explaining things if you didn't care about it (no offense, I'm just lazy).

 

You're looking for the exact string "" (case-insensitive). Regular expressions are overkill for that, but you need a count of matches and there is no built-in PHP function that can do that*.

So now you have to create a regular expression meaning "literally ''". Two things: expressions need delimiters and [ and ] are special characters. What you had before "worked" because preg_match() accepts a [] pair as delimiters, but that meant you were actually only searching for "img". If you had the string

To use img tags use "[img]".

there would have been two matches.

 

So the slashes in the expression are the new delimiters, preg_quote() turns an arbitrary string into something safe for regular expressions (turning the [ and ] into \[ and \]), and the /i flag means case-insensitive.

 

Now that you're in the Regex forum, take a look at the stickies we have here.

 

* substr_count() is case-sensitive.

 

Hmm, thanks for explaining! Helps alot, then just giving code, will help me in the future also.

 

Coulda sworn the /i meant case sensitive, ( wouldn't of worked then) I guess for regex it's a whole different ballgame and /i makes it work for that particular string, thanks Req.

 

 

Share this post


Link to post
Share on other sites

Coulda sworn the /i meant case sensitive

Case-insensitive ;)

Share this post


Link to post
Share on other sites

×
×
  • 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.