[help]preg_match_all pattern

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Eng_A_Moktar
Forum Newbie
Posts: 2
Joined: Wed Jul 08, 2009 12:35 am

[help]preg_match_all pattern

Post by Eng_A_Moktar »

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.
SvanteH
Forum Commoner
Posts: 50
Joined: Wed Jul 08, 2009 12:25 am

Re: [help]preg_match_all pattern

Post by SvanteH »

Adding the modifer 's' in the end of your pattern might help you. Looks like the problem for you right now is that it stops matching when a newline character is found.
Post Reply