在PHP中将html转换为纯文本

时间:2014-03-08 12:11:20

标签: php

我有一个包含这些下面内容的数组。

<p><span style="color:#000000"><span style="font-family:dejavu sans mono,monospace; font-size:11px">Popularly known as Ideator, among his close circles, Bikash is very passionate about re-engineering out</span><span style="font-family:dejavu sans mono,monospace; font-size:11px">-</span><span style="font-family:dejavu sans mono,monospace; font-size:11px">of-box ideas.</span></span><span style="color:rgb(34, 34, 34); font-family:dejavu sans mono,monospace; font-size:11px"> He loves travelling, reading, and is always up for a business processing challenge for innovative ideas.Loves getting hands dirty with real on-ground work.</span></p>

我必须将上面的内容转换为纯文本,如

 "Popularly known as Ideator, among his close circles, Bikash is very passionate about re-engineering out of-box ideas.He loves travelling, reading, and is always up for a business processing challenge for innovative ideas.Loves getting hands dirty with real on-ground work."

有没有办法将上面的内容转换为php中的纯文本。

3 个答案:

答案 0 :(得分:0)

尝试使用strip_tags

e.g。

$str = "<p>some test</p>";
$str = strip_tags($str);

答案 1 :(得分:0)

<强> strip_tags

(PHP 4,PHP 5)

  

strip_tags - 从字符串中删除HTML和PHP标记

WORKING DEMO

 <?php

    echo strip_tags('<p><span style="color:#000000"><span style="font-family:dejavu sans mono,monospace; font-size:11px">Popularly known as Ideator, among his close circles, Bikash is very passionate about re-engineering out</span><span style="font-family:dejavu sans mono,monospace; font-size:11px">-</span><span style="font-family:dejavu sans mono,monospace; font-size:11px">of-box ideas.</span></span><span style="color:rgb(34, 34, 34); font-family:dejavu sans mono,monospace; font-size:11px"> He loves travelling, reading, and is always up for a business processing challenge for innovative ideas.Loves getting hands dirty with real on-ground work.</span></p>');

    ?> 

<强>输出

  

通常被称为Ideator,在他的密切圈子中,Bikash非常   热衷于重新设计开箱即用的想法。他喜欢旅游,   阅读,并始终是一个业务处理挑战   创新的想法。爱好真正的实地工作。

答案 2 :(得分:0)

某些变体的正则表达式应该有所帮助。我建议首先使用online regex tester来获得正确的匹配组合,然后在php中部署它。

试试这个:

$string = '<p><span style="color:#000000"><span style="font-family:dejavu sans mono,monospace; font-size:11px">Popularly known as Ideator, among his close circles, Bikash is very passionate about re-engineering out</span><span style="font-fa...';

$result = preg_replace('/<([\s\S]*?)>/', '', $string);

echo $result;