获取<;a></a>;标签


Get the word between the <a></a> tag

如果我有一个html行,比如:

<a href="your.link-and-stuf.php" title="here your page title and stuf">this word</a>

我想用php把"这个词"去掉。我试过用str_replace(),但没有成功。因为链接发生了变化。

那么我该怎么做呢?

简单的解决方案是使用内置函数strip_tags,复杂的解决方案将使用正则表达式

条形标签实现

$str = '<a href="your.link-and-stuf.php" title="here your page title and stuf">this word</a>';
$strip = strip_tags($str);
echo $strip; // this word

正则表达式匹配

$str = '<a href="your.link-and-stuf.php" title="here your page title and stuf">this word</a>';
$strip = preg_replace("/<''/?a(''s+.*?>|>)/", "", $str); // removes only a tags
echo $strip; // this word

我会使用simplehtmldom这样的库。

代码可能看起来像:

$html = str_get_html('<a href="your.link-and-stuf.php" title="here your page title and stuf">this word</a>');
$text = $html->find('a', 0)->innerText;

我会使用DOMDocument

$doc = new DOMDocument();
$doc->loadHTML('<a href="your.link-and-stuf.php" title="here your page title and stuf">this word</a>');
echo $doc->getElementsByTagName('a')->item(0)->nodeValue;