note 48069 added to function.preg-match-all

From: Date: Fri, 10 Dec 2004 12:40:11 +0000
Subject: note 48069 added to function.preg-match-all
Groups: php.notes 
Request: Send a blank email to php-notes+get-81649@lists.php.net to get a copy of this message
$html = "<span>foo</span><span>bar</span>"; header('content-type: text/plain'); preg_match_all('/(<([\w]+)[^>]*>)(.*)(<\/\\2>)/', $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"; } Does anyone know how to match every single tag in the example above? I used the expression from the example, but it doesn't work for me, it works for <a>foo</a><b>bar</b> but not for multiple tags... <span>foo</span><span>bar</span> resulted in only one match... matched: <span>foo</span><span>bar</span> part 1: <span> part 2: foo</span><span>bar part 3: </span> I think the (.*) causes the problem because it matches every character which includes the 'foo</span><span>bar' piece of the string. ---- Manual Page -- http://www.php.net/manual/en/function.preg-match-all.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+48069 Delete -- http://master.php.net/manage/user-notes.php?action=delete+48069&report=yes Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+48069&report=yes&reason=bad+code Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+48069&report=yes&reason=spam Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+48069&report=yes&reason=useless+example Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+48069&report=yes&reason=contains+commercial+links Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+48069&report=yes&reason=useless+note Reject -- http://master.php.net/manage/user-notes.php?action=reject+48069&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#81649) next »