[help]preg_match_all pattern
Posted: Wed Jul 08, 2009 12:40 am
Hi, I wanna make a general pattern for example i have this string , i wanna capture the titles and links of this topic :
<?php
$str="<td valign='top'>
<center>
<a href='redirect-5321'>
Zito, Romo combo zero
<br><br>
<img src='images/img280745.png' border='0' width='190' height='190'></a>
</center>
<br>
</td></tr><tr>
<td valign='top'>
<center>
<a href='redirect-5322'>
NBA salary cap set at $57.7 million
<br><br>
<img src='images/img280746.png' border='0' width='190' height='190'></a>
</center>
<br>
</td></tr><tr>";
// define start and end
$title_start = ">"; $title_end = "<br><br>";
$link_start = "<a href='"; $link_end = "'";
$code = preg_match_all("/{$title_start}(.*?){$title_end}/", $str, $result1);
$code = preg_match_all("/{$link_start}(.*?){$link_end}/", $str, $result2, PREG_PATTERN_ORDER);
echo $result1[1][0];
echo "<br />";
echo $result2[1][0];
echo "<br />";
?>
in this example, title pattern doesn't work, because there are spaces and lines,
what i wanna say, how can i make my pattern wider and flexible, even works with tags or words ?
thanks.
<?php
$str="<td valign='top'>
<center>
<a href='redirect-5321'>
Zito, Romo combo zero
<br><br>
<img src='images/img280745.png' border='0' width='190' height='190'></a>
</center>
<br>
</td></tr><tr>
<td valign='top'>
<center>
<a href='redirect-5322'>
NBA salary cap set at $57.7 million
<br><br>
<img src='images/img280746.png' border='0' width='190' height='190'></a>
</center>
<br>
</td></tr><tr>";
// define start and end
$title_start = ">"; $title_end = "<br><br>";
$link_start = "<a href='"; $link_end = "'";
$code = preg_match_all("/{$title_start}(.*?){$title_end}/", $str, $result1);
$code = preg_match_all("/{$link_start}(.*?){$link_end}/", $str, $result2, PREG_PATTERN_ORDER);
echo $result1[1][0];
echo "<br />";
echo $result2[1][0];
echo "<br />";
?>
in this example, title pattern doesn't work, because there are spaces and lines,
what i wanna say, how can i make my pattern wider and flexible, even works with tags or words ?
thanks.