Forum Moderators: coopster

Message Too Old, No Replies

problems with preg match and regexb

         

dulldull

8:09 am on Oct 17, 2009 (gmt 0)

10+ Year Member



Hi,

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.

rocknbil

5:19 pm on Oct 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



you have three parameters there for preg_match, not sure what for. Anything other than the first two are for matches, arrays, flags, and offset [us.php.net].

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>';
?>

TheMadScientist

4:52 am on Oct 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Just as a 'heads up' on the ? following .* ... It inverts the 'greediness' of the expression... So, if /.*?/U is used the .* is a 'greedy' match, where if /.*?/ the .* is 'ungreedy'.

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.