仅显示更多内容的某些内容

时间:2012-10-13 05:57:28

标签: php html

我有像

这样的内容
<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个字符,只有

说明:“入门”包含您在设置计算机时可能要执行的任务列表。入门任务包括:

它应该排除标签和 如何实现这个????

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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

中的任何位置回显此数据

答案 2 :(得分: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实体。所以我从How to remove html special chars?

借了一个正则表达式

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