Forum Moderators: coopster

Message Too Old, No Replies

Help with Regular Expression

         

dermotirl

4:24 pm on Jun 22, 2006 (gmt 0)

10+ Year Member



I am using a regular expression to find all the content between the <table> and </table> tags. The regular expression is working fine if there is only one table in my html. However when there is more that one table
e.g. <table>content</table> <table>content</table> it returns the following and puts it into an array "<table>content</table><table>content</table>" but what I want to do is put <table>content</table> in one array element and the other <table>content</table> in another array element.

My regular expression is as follows
$content = htmlentities($string);
$pattern = "/(&lt;table&gt;)(.*)(&lt;\/table&gt;)/i";

if (preg_match($pattern, $content, $matches)) {
echo "A match was found.";
print_r ($matches);
} else {
echo "A match was not found.";
}

Thanks

Vali

5:24 pm on Jun 22, 2006 (gmt 0)

10+ Year Member



Try this:

$pattern = "/(&lt;table&gt;)(.*?)(&lt;\/table&gt;)/i";

dermotirl

8:08 am on Jun 23, 2006 (gmt 0)

10+ Year Member



The regular expression works fine, the problem i'm having is going through the code and adding all the tables and their content to an array.

I want the array as follows:
Array
(
[0] => Array
(
[0] => <table>Content1</table>
[1] => <table>Content2</table>
[n] => <table>Contentn</table>
)
)

eelixduppy

1:57 pm on Jun 23, 2006 (gmt 0)



Use preg_match_all [us3.php.net]. So it would then look something like this:


$pattern = "/<table.*>\s*.*\s*<\/table>/i";
if (preg_match_all($pattern, $string, $matches)) {
$count = count($matches[0]);
for($i = 0; $i < $count; $i++)
{
echo "find ".($i+1).": ".$matches[0][$i]."<br>";
}
} else {
echo "A match was not found.";
}

Good luck!