Php 如何解析一些html标记内容,并用一些单独的字符将它们分开,如|&引用;?
我有一个变量,比如:Php 如何解析一些html标记内容,并用一些单独的字符将它们分开,如|&引用;?,php,html-parsing,Php,Html Parsing,我有一个变量,比如: $content = ' <i class="fa fa-hashtag" aria-hidden="true"> <a href="http://example.com/tag/digital_marketing">digital marketing</a> </i> <i class="fa fa-hashtag" aria-hidden="true"> <a href="http://e
$content = '
<i class="fa fa-hashtag" aria-hidden="true">
<a href="http://example.com/tag/digital_marketing">digital marketing</a>
</i>
<i class="fa fa-hashtag" aria-hidden="true">
<a href="http://example.com/tag/seo">seo</a>
</i>
<i class="fa fa-hashtag" aria-hidden="true">
<a href="http://example.com/tag/internet_marketing">internet marketing</a>
</i>';
$content='1!'
';
如何提取标签内容,如下所示:
数字营销|搜索引擎优化|互联网营销
谢谢。您试图访问的所有内容都存在于
标签中,该标签仅为i
的子项。在这里,我使用DOMDocument
来实现所需的结果
当我使用更精确的xpath时,例如://div[@id='tags']]/a没有成功。你知道Sahil吗?@MohammadJ你能分享HTML
?您的XPath
看起来很好,请将'
更改为“
,然后重试。
<?php
ini_set('display_errors', 1);
$string=<<<HTML
<i class="fa fa-hashtag" aria-hidden="true">
<a href="http://example.com/tag/digital_marketing">digital marketing</a>
</i>
<i class="fa fa-hashtag" aria-hidden="true">
<a href="http://example.com/tag/seo">seo</a>
</i>
<i class="fa fa-hashtag" aria-hidden="true">
<a href="http://example.com/tag/internet_marketing">internet marketing</a>
</i>
HTML;
$domDocument = new DOMDocument();
$domDocument->loadHTML($string);
$domXPath = new DOMXPath($domDocument);
$results = $domXPath->query("//i/a");
foreach($results as $result)
{
$data[]= $result->textContent;
}
echo implode("|",$data);