note 92018 added to function.preg-match-all
| From: | alaattin at gmail dot com | Date: | Sun, 05 Jul 2009 15:48:38 +0000 |
| Subject: | note 92018 added to function.preg-match-all | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-157940@lists.php.net to get a copy of this message | ||
ATTENTION!
The code supplied in Example 2 is erronous,
If the document contains more than one occurences of a tag, than it fails:
$html = '<h1>Heading Largest First</h1><h2>Heading Larger
First</h2><a href=howdy.html>click me</a><h1>Heading Largest
Second</h1>Some text<h3>Heading Normal</h3>';
preg_match_all("/(<([\w]+)[^>]*>)(.*)(<\/\\2>)/", $html, $matches,
PREG_SET_ORDER);
foreach ($matches as $val) {
echo "matched: " . $val[0] . "\n";
echo "part 1: " . $val[1] . "\n";
echo "part 2: " . $val[3] . "\n";
echo "part 3: " . $val[4] . "\n\n";
}
?>
The ouput is:
matched: <h1>Heading Largest First</h1><h2>Heading Larger First</h2><a
href=howdy.html>click me</a><h1>Heading Largest Second</h1>
part 1: <h1>
part 2: Heading Largest First</h1><h2>Heading Larger First</h2><a
href=howdy.html>click me</a><h1>Heading Largest Second
part 3: </h1>
matched: <h3>Heading Normal</h3>
part 1: <h3>
part 2: Heading Normal
part 3: </h3>
The reqular expression is missing a question mark, the corrected snippet should be:
$html = '<h1>Heading Largest First</h1><h2>Heading Larger
First</h2><a href=howdy.html>click me</a><h1>Heading Largest
Second</h1>Some text<h3>Heading Normal</h3>';
preg_match_all("/(<([\w]+)[^>]*>)(.*?)(<\/\\2>)/", $html, $matches,
PREG_SET_ORDER);
foreach ($matches as $val) {
echo "matched: " . $val[0] . "\n";
echo "part 1: " . $val[1] . "\n";
echo "part 2: " . $val[3] . "\n";
echo "part 3: " . $val[4] . "\n\n";
}
?>
Then the output will be:
matched: <h1>Heading Largest First</h1>
part 1: <h1>
part 2: Heading Largest First
part 3: </h1>
matched: <h2>Heading Larger First</h2>
part 1: <h2>
part 2: Heading Larger First
part 3: </h2>
matched: <a href=howdy.html>click me</a>
part 1: <a href=howdy.html>
part 2: click me
part 3: </a>
matched: <h1>Heading Largest Second</h1>
part 1: <h1>
part 2: Heading Largest Second
part 3: </h1>
matched: <h3>Heading Normal</h3>
part 1: <h3>
part 2: Heading Normal
part 3: </h3>
----
Server IP: 69.147.83.197
Probable Submitter: 94.54.1.63
----
Manual Page -- http://www.php.net/manual/en/function.preg-match-all.php
Edit -- https://master.php.net/note/edit/92018
Del: integrated -- https://master.php.net/note/delete/92018/integrated
Del: useless -- https://master.php.net/note/delete/92018/useless
Del: bad code -- https://master.php.net/note/delete/92018/bad+code
Del: spam -- https://master.php.net/note/delete/92018/spam
Del: non-english -- https://master.php.net/note/delete/92018/non-english
Del: in docs -- https://master.php.net/note/delete/92018/in+docs
Del: other reasons-- https://master.php.net/note/delete/92018
Reject -- https://master.php.net/note/reject/92018
Search -- https://master.php.net/manage/user-notes.php