Symfony 2 Dom Crawler:如何在 Element 中只获取 text() [英] Symfony 2 Dom Crawler: how to get only text() in Element

查看:24
本文介绍了Symfony 2 Dom Crawler:如何在 Element 中只获取 text()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Dom Crawler 只获取文本(不带标签).

$html = EOT<<<<<div class="coucu">获取描述 <span>Coucu</span>

EOT;$crawler = new Crawler($html);$crawler = $crawler->filter('.coucu')->first()->text();

输出: 获取描述拼凑

我想输出(仅):获取描述

更新:

我为此找到了一个解决方案:(但这确实是一个糟糕的解决方案)

<代码>...$html = $crawler->filter('.coucu')->html();//在 https://php.net/strip_tags 中使用 strip_tags_content$html = strip_tags_content($html,'span');

解决方案

遇到了同样的情况.我最终去了:

$html = $crawler->filter('.coucu')->html();$html =explode("<span", $html);回声修剪($ html [0]);

Using Dom Crawler to get only text (without tag).

$html = EOT<<<
  <div class="coucu">
    Get Description <span>Coucu</span>
  </div>
EOT;

$crawler = new Crawler($html);
$crawler = $crawler->filter('.coucu')->first()->text();

output: Get Description Coucu

I want to output (only): Get Description

UPDATE:

I found a solution for this: (but it's really bad solution)

...
$html = $crawler->filter('.coucu')->html();
// use strip_tags_content in https://php.net/strip_tags
$html = strip_tags_content($html,'span');

解决方案

Ran into the same situation. I ended up going with:

$html = $crawler->filter('.coucu')->html();
$html = explode("<span", $html);
echo trim($html[0]);

这篇关于Symfony 2 Dom Crawler:如何在 Element 中只获取 text()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆