【发布时间】:2015-07-18 22:28:01
【问题描述】:
使用 Dom Crawler 只获取文本(不带标签)。
$html = EOT<<<
<div class="coucu">
Get Description <span>Coucu</span>
</div>
EOT;
$crawler = new Crawler($html);
$crawler = $crawler->filter('.coucu')->first()->text();
输出: 获取描述 Coucu
我想输出(仅): 获取描述
更新:
我找到了一个解决方案:(但这确实是一个糟糕的解决方案)
...
$html = $crawler->filter('.coucu')->html();
// use strip_tags_content in https://php.net/strip_tags
$html = strip_tags_content($html,'span');
【问题讨论】:
-
不,我没有使用 jQuery
-
我不认为有这种方法,但你可以尝试 $text = $crawler->filter('.coucu')->first()->extract(array('_text '));我相信它会返回相同的结果,但仍然值得一试
-
我使用了提取函数()。但这行不通。
-
我猜
strip_tags_content来自gist.github.com/marcanuy/7651298。我个人不喜欢 HTML 的正则表达式,它们会导致坏事 (stackoverflow.com/questions/590747/…)。
标签: symfony domcrawler