jake2891 Posted May 14, 2009 Share Posted May 14, 2009 Hi I have been going through documentation on regular expressions and still dont fully understand what this expression is doing. can someone please explain it for me. thanks $str = preg_replace('/^([ ]+<[^>]+>.+)<\/[^>]+>$/', '$1', $str); Quote Link to comment https://forums.phpfreaks.com/topic/158164-solved-need-help-understanding-what-this-expression-does/ Share on other sites More sharing options...
nrg_alpha Posted May 14, 2009 Share Posted May 14, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/158164-solved-need-help-understanding-what-this-expression-does/#findComment-834327 Share on other sites More sharing options...
jake2891 Posted May 14, 2009 Author Share Posted May 14, 2009 thanks for taking the time to clear things up for me. that makes alot more sense now. Quote Link to comment https://forums.phpfreaks.com/topic/158164-solved-need-help-understanding-what-this-expression-does/#findComment-834339 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.