note 52537 added to function.preg-match-all

From: Date: Thu, 05 May 2005 04:46:49 +0000
Subject: note 52537 added to function.preg-match-all
Groups: php.notes 
Request: Send a blank email to php-notes+get-88946@lists.php.net to get a copy of this message
As seen in example 2: preg_match_all("/(<([\w]+)[^>]*>)(.*)(<\/\\2>)/s", $html, $matches); Only works on tags with content like <b>content</b> but doesn't list "empty" tags like <b></b> To get around this you could use: preg_match_all("/(<([\w]+)[^>]*>)(.*?)(<\/\\2>)/s", $html, $matches); Which works on tags with no content However this does give issues with nested tags like: <?php $html = "<table><tr><td> <table><tr><td><b>bold text</b></td></tr></table> </td></tr></table>"; preg_match_all("/(<([\w]+)[^>]*>)(.*?)(<\/\\2>)/s", $html, $matches); for ($i=0; $i< count($matches[0]); $i++) { echo "matched: " . $matches[0][$i] . "\n"; echo "part 1: " . $matches[1][$i] . "\n"; echo "part 2: " . $matches[3][$i] . "\n"; echo "part 3: " . $matches[4][$i] . "\n\n"; } ?> matched: <table><tr><td> <table><tr><td><b>bold text</b></td></tr></table> part 1: <table> part 2: <tr><td> <table><tr><td><b>bold text</b></td></tr> part 3: </table> I didn't find a correct solution as of yet, maybe someone else will. ---- Manual Page -- http://www.php.net/manual/en/function.preg-match-all.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+52537 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52537&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52537&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52537&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52537&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52537&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+52537&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#88946) next »