DomXPath find element with exact name

1

I'm having a problem trying to find an element with the exact name.

<?
//...
@$DOM = new DOMDocument;
@$DOM->loadHTML($html);
@$finder = new DomXPath($DOM); 

foreach($finder->query("//a[contains(@title, '".$info[$i]."')]") as $link){
            echo $l[$i] = $link->getAttribute('href').'<br>';
}
//...    
?>

Your response is several links (caught from getAtrribute() ), no problem.

Where is the problem?

  

I would like to ONLY get what title is equal to $info[$i] .

Exemplifying (note the title !):

<a href='1.html' title="biscoito"></a>
<a href='2.html' title="biscoito bolacha"></a>
<a href='3.html' title="bolacha"></a>

If $info[$i] had the value of " biscoito ", it would be returned:

1.html //oriundo do biscoito
2.html //oriundo do biscoito bolacha

Desired result:

1.html

Solution I need:

There is some function (which I do not know) that only filters the name equally, but not only contains it. Because I need only where title is equal to cookie , but not the cookie cookie .     

asked by anonymous 12.01.2016 / 14:29

1 answer

0

I was able to find a solution to the problem.

Solution:

<?
//...
@$DOM = new DOMDocument;
@$DOM->loadHTML($html);
@$finder = new DomXPath($DOM); 

foreach($finder->query("//a[@title='".$info[$i]."']") as $link){
            echo $l[$i] = $link->getAttribute('href').'<br>';
}
//...    
?>

In short:

Replace "//a[contains(@title, 'biscoito')]" to "//a[@title='biscoito']" .

Explanation:

  

The = will only display what is equal, rather than just contain the string. That's exactly what I was looking for.

Note:

There is another function that is starts-with (becoming: //a[starts-with(@title,"biscoito")] ), in this case it would not do any good, but if you have the same problem you can test it. In the case of searching for cookie , I would only display one, instead of two, for example.

    
12.01.2016 / 14:51