将HTML链接与特定标题匹配[关闭]

How can I retrieve a URL from an HTML link with a specific begining title?

eg.:

<a href="http://urltoretrieve.ext/" title="specific title rest of all title">something</a>
<a href="http://otherurl.ext/" title="a generic title">somethingelse</a>

and use PHP to retrieve:

http://urltoretrieve.ext/

Thanks!

You can use https://gist.github.com/1358174 and this XPath:

//a[starts-with(@title, "specific title")]/@href

This query means:

//a                      find all a elements in the html
[                        that
starts-with(             
    @title               has a title attribute
    'specific-title'     starting with this value
)                        
]                        
/@href                   and return their href attribute

Example (demo):

$result = xpath_match_all(
    '//a[starts-with(@title, "specific title")]/@href', 
    $yourHtmlAsString
);

Output:

array(2) {
  [0]=>
  array(1) {
    [0]=>
    string(38) "<href>http://urltoretrieve.ext/</href>"
  }
  [1]=>
  array(1) {
    [0]=>
    string(25) "http://urltoretrieve.ext/"
  }
}

The result is an array containing the serialized innerHTML and outerHTML of the found attribute nodes. If you dont understand what a node is, check DOMDocument in php

Also see How do you parse and process HTML/XML in PHP?