Jump to content

[SOLVED] need help understanding what this expression does


Recommended Posts

The basic rundown is:

 

^ from the beginning of string

( start a new capture

[ ]+ a character class that looks to see if the current character is a space (one or more times, due to the +)

<[^>]+> followed by <, then anything that is not > (due to the [^>]) one or more times, then a >

.+ the dot is a dot_match_all wildcard character that matches anything that is not a newline by default (one or more times).

) close capture

<\/[^>]+> followed by <, then / (this slash needs to be escaped using \ before it, as the delimiters surrounding the entire pattern is the / character), then anything that is not a > (one or more times), then finally >

$ end of string.

 

So anything that is within the set of parenthesis that the pattern matches gets stored into a regex variable called $1 (which is used as the second parameter in the regex as a replacement in $str.

 

There are some bad forms with this though.. by example, if you look for a single character space (as in the space within the first character class [ ]), you don't need to encase it in a character class.. simply providing a literal space (or the hex vale \x20). Other aspects like .+ means its greedy.. match anything up to a newline. But as a result, the regex engine will now start to backtrack to accommodate what comes after that in the pattern (which can create accuracy / speed problems).

 

you can learn more about regex by viewing the following links:

 

regex tutorials

weblogtools

phpfreaks regex resources

phpfreaks regex tutorial

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.