Forum Moderators: coopster
I'm trying to put put the following coding into a variable with preg_match, but I can't only retrieve parts of it.
the code is:
preg_match('#<div id="a">(.*?)</div>#si',$subject,$a);
the content i want to process:
<div id="a">
<table id="a" cellpadding="1" cellspacing="1">
<thead>
<tr><th colspan="3">A</th></tr>
</thead>
<tbody>
.
.
.
<table id="troops" cellpadding="1" cellspacing="1">
<thead>
<tr<th colspan="3">B/th></tr>
</thead>
<tbody>
.
.
.
</tbody>
</table>
</div>
Did i make any mistake with the regex?
any advice is appreciated.
The only thing I changed was moving start and end div into variables, so I had to use double quotes in the match to interpolate them.
I can't get it to not work. :-) It must be something else, or that third parameter. Although the quantifier is not necessary, (.*?), it also works with just .* there.
<html><head><title>test</title></head><body>
<?php
$start_div = '<div id="a">';
$end_div = '</div>';
$subject = '
<div id="a"><table id="a" cellpadding="1" cellspacing="1">
<thead>
<tr><th colspan="3">A</th></tr>
</thead>
<tbody>
.
.
.
<table id="troops" cellpadding="1" cellspacing="1">
<thead>
<tr<th colspan="3">B/th></tr>
</thead>
<tbody>
.
.
.
</tbody>
</table></div>
';
$code = htmlentities($subject);
echo $code;
if (preg_match("#$start_div(.*?)$end_div#si",$subject)) {
// or, use if (preg_match("#$start_div.*$end_div#si",$subject)) {
echo '<p>We have a match</p>';
}
else { echo '<p>No match</p>'; }
echo '</body></html>';
?>
My guess as to why it does not appear to work is even if you print_r($a) the html does not show, unless you view the source...
<html><head><title>test</title></head><body>
<?php
$subject = '
<div id="a">
<table id="a" cellpadding="1" cellspacing="1">
<thead>
<tr><th colspan="3">A</th></tr>
</thead>
<tbody>
.
.
.
<table id="troops" cellpadding="1" cellspacing="1">
<thead>
<tr><th colspan="3">B</th></tr>
</thead>
<tbody>
.
.
.
</tbody>
</table>
</div>
';
$code = htmlentities($subject);
echo $code;
if (preg_match('#<div id="a">(.*?)</div>#si',$subject,$a)) {
echo '<p>We have a match</p>';
echo htmlentities($a[0])."<br /><br />";
echo htmlentities($a[1]);
}
else { echo '<p>No match</p>'; }
echo '</body></html>';
?>
$a[1] appears to be the one you want to use.