Jump to content

preg_match explanation


burtybob

Recommended Posts

Hi,

 

I don't know if this should be here or in the regex help section so if it is in the wrong section please move it.

 

I have this preg_match("/\/warn (.*)/",$shout,$matches)

 

I understand almost everything in there except the ""/\/warn (.*)/"" section...

It takes input from a shoutbox input. So you put in "/warn burtybob" and it will warn burtybob and I understand all the rest of the script that does that.

 

I would like to know what the "/\" is for, I presume it is something to do with character escaping.

I would like to know what the "(.*)/" stuff does as well. I would like to say thanks in advance for explaning this to me.

 

Thanks in advance,

 

Ben

 

Link to comment
Share on other sites

The first forward slash is the opening delimiter, all preg functions will have an opening and closing delimiter. the \/ is an escaped forward slash. This means to take it as a literal character ie match "/warn" the brackets of the next part tells it to capture it as a seperate group in the $mathces array. The . means 'any character' (with a few exceptions) and the * means repeat the character before 0 or more times. Then the final forward slash is the closing delimiter.

 

Checkout the link provided by Mchl to learn about regular expressions.

Link to comment
Share on other sites

@burtybob

 

This is why I don't like using the forward slash as delimiters, as that character is common for paths and whatnot. Using something other than the forward slash negates the need to escape them elsewhere in the pattern.. I typically use the hash(#) as my delimiters.. but you can choose from an assortment of delimiters, like ~....~ or !...!, etc..

 

There's a caveat on using .* This is typically frowned upon for potential speed / accuracy purposes. You can read up about that (in the form of .+, but pretty much the same principal) here (post #14 illustrates the pitfalls).

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.