One of the advantages of PCRE or POSIX is that some special constructs are supported. For instance, usually regular expressions are matched greedily. Take, for instance, this regular expression: <.*> When trying to match this in the following string: <p>Sex, drugs and <b>PHP</b>.</p> what do you get? You get the complete string. Of course, the pattern also matches on <p>, but regular expressions try to match as much as possible. Therefore, you usually have to do a clumsy workaround, such as <[^>]*>. However, it can be done more easily. You can use the ? modifier after the * quantifier to activate nongreedy matching. Finding All Tags Using Non-greedy PCRE (non-greedy.php)<?php $string = '<p>Sex, drugs and <b>PHP</b>.</p>'; preg_match_all('/<.*?>/', $string, $matches); foreach ($matches[0] as $match) { echo htmlspecialchars("$match "); } ?> Which outputs: <p> <b> </b> </p> |