I want all the trs tds and ths extracted <?php $text = ' <table> <tr> <th>Unit Type</th> <th>Availability</th> <th>Rates</th> </tr> <tr> <td>One Bedroom</td> <td>Call for Availability</td> <td>hello</td> </tr> <tr> <td>One Living Room</td> <td>Call for Availability</td> <td>hello</td> </tr> </table>'; $extract_th="#<th.*>(.+)</th#Ui"; $extract_tr="/<tr>(.*)<\/tr>/isU"; $extract_td="/<td.*>(.*)<\/td>/Ui"; echo $text."<br />\n"; preg_match_all($extract_tr, $text, $match_tr, PREG_SET_ORDER); //print_r($match_tr[1][1]); for($i=0; $i<count($match_tr); $i++){ for($td=0; $td<count($match_tr[$i]); $td++){ preg_match_all($extract_td, $match_tr[$i][$td], $match_td, PREG_SET_ORDER); print_r($match_td[$i]); } } ?> PHP: I'm getting: Array ( [0] => <td>Call for Availability</td> [1] => Call for Availability ) Array ( [0] => <td>Call for Availability</td> [1] => Call for Availability ) Array ( [0] => <td>hello</td> [1] => hello ) Array ( [0] => <td>hello</td> [1] => hello ) Code (markup):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title></title> </head> <body> <?php $text = '<table><tr><th>Unit Type</th><th>Availability</th><th>Rates</th></tr><tr><td>One Bedroom</td><td>Call for Availability</td><td>hello</td></tr><tr><td>One Living Room</td><td>Call not for Availability</td><td>hello</td></tr></table>'; $extract_th="#<th.*>(.+)</th#Ui"; $extract_tr="/<tr>(.*)<\/tr>/isU"; $extract_td="/<td.*>(.*)<\/td>/Ui"; echo $text."<br />\n"; preg_match_all($extract_tr, $text, $match_tr, PREG_SET_ORDER); //print_r($match_tr[1][1]); //var_dump($match_tr); //echo count($match_tr); //print_r($match_tr); //print_r($match_tr[0][1]); //print_r($match_tr[1][1]); //print_r($match_tr[2][1]); preg_match($extract_td, $match_tr[0][1], $match_th); print_r($match_th); for($td=1; $td<count($match_tr); $td++){ //preg_match_all($extract_td, $match_tr[$td][1], $match_td, PREG_SET_ORDER); echo "[".$match_tr[$td][1]."]<br />\n"; preg_match($extract_td, $match_tr[$td][1], $match_td); print_r($match_td[$td]); } ?> </body> </html> PHP: The output: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title></title> </head> <body> <table><tr><th>Unit Type</th><th>Availability</th><th>Rates</th></tr><tr><td>One Bedroom</td><td>Call for Availability</td><td>hello</td></tr><tr><td>One Living Room</td><td>Call not for Availability</td><td>hello</td></tr></table><br /> Array ( ) [<td>One Bedroom</td><td>Call for Availability</td><td>hello</td>]<br /> One Bedroom[<td>One Living Room</td><td>Call not for Availability</td><td>hello</td>]<br /> </body> </html> Code (markup): It's not working.
as rows starts with tr start finding them first, then find the rest : preg_match_all('@<tr>(.*)<\/tr>@siU',$text,$out); Code (markup): This part will give you all tr's. Now use $out[0] as the new string to get other elements inside it by doing preg_match_all again. hope this helps.
Both tr reg exp work fine and the same way, same result. It's the td that doesn't work or I haven't figured out how it should work in the for loop. //$extract_tr="/<tr>(.*)<\/tr>/isU"; $extract_tr='@<tr>(.*)<\/tr>@siU'; $extract_td="/<td.*>(.*)<\/td>/Ui"; PHP:
<?php $text = '<table><tr><th>Unit Type</th><th>Availability</th><th>Rates</th></tr><tr><td>One Bedroom</td><td>Call for Availability</td><td>hello</td></tr><tr><td>One Living Room</td><td>Call not for Availability</td><td>hello</td></tr></table>'; $extract_th="#<th.*>(.+)</th#Ui"; //$extract_tr="/<tr>(.*)<\/tr>/isU"; $extract_tr='@<tr>(.*)<\/tr>@siU'; $extract_td="/<td.*>(.*)<\/td>/Ui"; echo $text."<br />\n"; preg_match_all($extract_tr, $text, $match_tr, PREG_SET_ORDER); //print_r($match_tr[1][1]); //var_dump($match_tr); //echo count($match_tr); print_r($match_tr); echo "[".$match_tr[0][1]."]<br />\n"; echo "[".$match_tr[1][1]."]<br />\n"; echo "[".$match_tr[2][1]."]<br />\n"; //preg_match($extract_td, $match_tr[0][1], $match_th); //print_r($match_th); for($td=1; $td<count($match_tr); $td++){ //preg_match_all($extract_td, $match_tr[$td][1], $match_td, PREG_SET_ORDER); //echo "[".$match_tr[$td][1]."]<br />\n"; preg_match($extract_td, $match_tr[$td][1], $match_td); print_r($match_td); } ?> PHP: Why is it that the preg_match($extract_td, $match_tr[$td][1], $match_td); print_r($match_td); PHP: in the for loop prints and not the whole thing? That's my question.
$content = '<table><tr><th>Unit Type</th><th>Availability</th><th>Rates</th></tr><tr><td>One Bedroom</td><td>Call for Availability</td><td>hello</td></tr><tr><td>One Living Room</td><td>Call not for Availability</td><td>hello</td></tr></table>'; preg_match_all('@<t[r|h|d]>(.*)<\/t[r|h|d]>@Uism', $content, $result); print_r($result); PHP: Hope this helps.
I like the fact that it's short but it's showing tags with it: Array ( [0] => Array ( [0] => <tr><th>Unit Type</th> [1] => <th>Availability</th> [2] => <th>Rates</th> [3] => <tr><td>One Bedroom</td> [4] => <td>Call for Availability</td> [5] => <td>hello</td> [6] => <tr><td>One Living Room</td> [7] => <td>Call not for Availability</td> [8] => <td>hello</td> ) [1] => Array ( [0] => <th>Unit Type [1] => Availability [2] => Rates [3] => <td>One Bedroom [4] => Call for Availability [5] => hello [6] => <td>One Living Room [7] => Call not for Availability [8] => hello ) ) PHP:
preg_match_all("@<t[r|h|d]>([a-zA-Z0-9 ]*)<\/t[r|h|d]>@Uism", $content, $result); PHP: This should work better.