2012-07-21 56 views

回答

-2

您可以使用preg_match()。这里有一个例子:

$link = '<a href="http://www.coursesweb.net" title="www.CoursesWeb.net">www.CoursesWeb.net</a>'; 
if(preg_match('/\<a([^\>]*)\>(.*?)\<\/a\>/i', $link, $mc)) { 
    echo $mc[2];   // www.CoursesWeb.net 
} 
+0

请不要使用正则表达式解析HTML,因为它会[驱动你的į̷̷͚̤̤̖̦͍͗̒̈̅̄n̨͖͓̹͍͎͔͈̝͐ͪ͛̄͛ṣ̷̵̞̦ͤ̅̉̋ͪ͑͛ͥ͜a̷̘͖̮͔͎͛̇̏̒͆̆͘n͇͔̤̼͙̩͖̭ͤ͋̉͌͟eͥ͒͆ͧͨ̽͞҉̹͍̳̻͢](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 )。改为使用[HTML解析器](http://stackoverflow.com/questions/292926/robust-mature-html-parser-for-php)。 – 2012-07-21 15:09:25

0

您可以直接使用XPath访问一个DOMText节点:

$xpath = new DOMXPath($dom_document); 
$node = $xpath->query('//a/text()')->item(0); 
echo $node->textContent; // text