Bhaamb Bhaamb - 10 days ago 5
PHP Question

PHP - preg_match unable to get all elements from html url

I have been trying to get the innertext of html tag from a url (defimedia.info) but i get only 1 output. The code i tried is:

$html = file_get_contents("http://www.defimedia.info");
preg_match("'<h3>(.*?)<h3>'si", $html, $match);
echo($match[1]);


even when i try to use foreach or i try to use $match[2], it does not work. Any help would certainly be appreciated.

regards
bhaamb

Answer

you need preg_match_all function. Documented here http://php.net/manual/en/function.preg-match-all.php

try like this.

<?php
$html = file_get_contents("http://www.defimedia.info");
preg_match_all('/<h3>(.*?)<h3>/si', $html, $match);
print_r($match);
?>
Comments