Crystal Cruises
Crystal Cruises

Reputation: 41

Retrieve a href titles containing a specific string in url php

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];
    }

Upvotes: 2

Views: 95

Answers (2)

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'));
}

Upvotes: 1

user399666
user399666

Reputation: 19899

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.

Upvotes: 0

Related Questions