2

i have the following code. and i want to retrieve only the a href titles , that have /movie/ within url.

 function get_a_contentmovies(){
          $h1count = preg_match_all("/(<a.*>)(\w.*)(<.*>)/ismU",$this->DataFromSite,$patterns);
        return $patterns[2];
    }

2 Answers2

1

You can use DOMXpath like this:

$dom = new DomDocument();
$dom->loadHTML($string);
$xpath = new DOMXpath($dom);
$elements = $xpath->query("//a[contains(@href, '/movie/')]");

foreach($elements as $el) {
    var_dump($el->getAttribute('title'));
}
0

Using Regex to parse (x)HTML is a bad idea. You should use a DOM parser such as DomDocument. Have a look at this topic.

Community
  • 1
  • 1
Wayne Whitty
  • 19,513
  • 7
  • 44
  • 66