我有一个变量,例如:
$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>';
如何提取这样的标签内容: 数字营销|搜索引擎优化|互联网营销
谢谢。
最佳答案
您尝试访问的所有内容都存在于 a
标记中,该标记只是 i
的子标记。在这里,我使用 DOMDocument 来实现所需的结果。
<?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);
关于php - 如何解析一些html标签内容并用一些特殊字符(如 "|")分隔它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44680027/