php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #48806 Typo on preg_match_all manual page
Submitted: 2009-07-05 16:16 UTC Modified: 2009-07-19 23:59 UTC
From: alaattin at gmail dot com Assigned:
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS: Debian Linux (Lenny)
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: alaattin at gmail dot com
New email:
PHP Version: OS:

 

 [2009-07-05 16:16 UTC] alaattin at gmail dot com
Description:
------------
The code supplied on http://tr.php.net/manual/en/function.preg-match-all.php 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>


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-07-19 23:58 UTC] svn@php.net
Automatic comment from SVN on behalf of rasmus
Log: Fixed bug #48806

Revision: http://svn.php.net/viewvc?view=revision&revision=284401
 [2009-07-19 23:59 UTC] rasmus@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 [2020-02-07 06:10 UTC] phpdocbot@php.net
Automatic comment on behalf of rasmus
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=e3a5bb2767bbb9a47f3cf0d0c556b2d2d969d09c
Log: Fixed bug #48806
 [2020-02-07 09:57 UTC] phpdocbot@php.net
Automatic comment on behalf of rasmus
Revision: http://git.php.net/?p=doc/de.git;a=commit;h=e3336e75ab2d0fa331bb5d01bfb9d732ad9c1922
Log: Fixed bug #48806
 [2020-02-07 14:18 UTC] phpdocbot@php.net
Automatic comment on behalf of rasmus
Revision: http://git.php.net/?p=doc/es.git;a=commit;h=ddca507d3a694b63063ed67274954175f9f16600
Log: Fixed bug #48806
 [2020-12-30 10:46 UTC] nikic@php.net
Automatic comment on behalf of rasmus
Revision: http://git.php.net/?p=doc/tr.git;a=commit;h=51630f789766055a4c819222df55c7aa380d5dad
Log: Fixed bug #48806
 [2020-12-30 10:47 UTC] nikic@php.net
Automatic comment on behalf of rasmus
Revision: http://git.php.net/?p=doc/zh.git;a=commit;h=66feb1d2ba2ef971df734346803e393db9274d3e
Log: Fixed bug #48806
 [2020-12-30 11:09 UTC] nikic@php.net
Automatic comment on behalf of rasmus
Revision: http://git.php.net/?p=doc/es.git;a=commit;h=8e2f8d73352cbc30ac41928901531b7f495203ab
Log: Fixed bug #48806
 [2020-12-30 11:15 UTC] nikic@php.net
Automatic comment on behalf of rasmus
Revision: http://git.php.net/?p=doc/de.git;a=commit;h=757ab879dcd1379391b50d4cc8912aebd91582bc
Log: Fixed bug #48806
 [2020-12-30 11:21 UTC] nikic@php.net
Automatic comment on behalf of rasmus
Revision: http://git.php.net/?p=doc/it.git;a=commit;h=c4e96126c732a873c583e74c48e3eefde5ef3cb9
Log: Fixed bug #48806
 [2020-12-30 11:59 UTC] nikic@php.net
Automatic comment on behalf of rasmus
Revision: http://git.php.net/?p=doc/ja.git;a=commit;h=c3bce4acfe366006c4cb1bf90e49d94a8ad55e4a
Log: Fixed bug #48806
 [2020-12-30 12:47 UTC] nikic@php.net
Automatic comment on behalf of rasmus
Revision: http://git.php.net/?p=doc/ru.git;a=commit;h=96957adc6a9225989f82c5bfc17846e5752af802
Log: Fixed bug #48806
 [2020-12-30 12:50 UTC] nikic@php.net
Automatic comment on behalf of rasmus
Revision: http://git.php.net/?p=doc/fr.git;a=commit;h=d8629a140e4638f37d20f5bb0a3f98d8c79e8326
Log: Fixed bug #48806
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Thu Jun 18 05:00:01 2026 UTC