Jump to content

help preg_retch


Monkuar

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?

Link to comment
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

Link to comment
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.

Link to comment
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.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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