1

我有一个内容

<p>
<strong><span style="font-family: monospace; white-space: pre-wrap; ">Description:Getting Started contains a list of tasks you might want to perform when you set up your computer. Tasks in Getting Started include: &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;  Dnescription:Getting Started contains a list of tasks you might want to perform when you set up your computer. Tasks in Getting Started include:</span></strong></p>

现在我只想选择 50 个字符

描述:Getting Started 包含您在设置计算机时可能要执行的任务列表。入门中的任务包括:

它应该排除标签以及如何实现这一点????

4

3 回答 3

0

如果您只想删除标签,您可以使用strip_tags($text);.
echo substr(str_replace("&nbsp;","",strip_tags($yourText),0,50))会打印你所期望的。

于 2012-10-13T06:01:39.540 回答
0

您应该在 PHP 中执行此操作,如下例所示

$data = '<b>Description: </b> Getting Started contains a list of tasks you might want to perform when you set up your computer. Tasks in Getting Started include:'

// remove html tags
$data = strip_tags($data);

// remove all &nbsp;

$data = preg_replace("/\[(.*?)\]\s*(.*?)\s*\[\/(.*?)\]/", "[$1]$2[/$3]", html_entity_decode($data));

OR

$data = trim(str_replace('&nbsp;','',$data));

//limit characters

$data = substr($data,0,50);

现在您可以在 html 中的任何位置回显此数据

于 2012-10-13T06:02:14.407 回答
0
$tmp = "<p><strong><span style=\"font-family: monospace; 
       white-space: pre-wrap;\">Description:Getting Started contains a list 
       of tasks you might want to perform when you set up your computer. Tasks 
       in Getting Started include: &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;
       &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;
       Dnescription: Getting Started contains a list of tasks you might want 
       to perform when you set up your computer. Tasks in Getting Started 
       include:</span></strong></p>";

$tmp = preg_replace("/&#?[a-z0-9]{2,8};/i","",$tmp);

$final = substr(strip_tags($tmp),0,50);

编辑:

如前所述,这不会删除 html 实体。所以我从如何删除 html 特殊字符中借了一个正则表达式?

现在,$final包含您想要的输出。

于 2012-10-13T06:05:37.340 回答