我在PHP中计算单词有问题,这是我的代码:
$word=substr(stripslashes(strip_tags($row['short_story'], '<a></a>')), 0,100 )."...";
$tpl->set( '{word}',$word);
在该代码上我只能在我的结果中显示URL链接,我需要显示完整的样式和HTML代码,所以我改为:
$word = substr( stripslashes (strip_tags($row['short_story'], '<a><b><i><u><br></a><div></div>')), 0,400 )."...";
我改变了这个:
<a></a>
为:
<a><b><i><u><br></a><div></div>
但问题是,如果我想显示我的结果是正确的,我必须设置超过400-500的字数限制!因为100 0到200字的限制,HTML代码将被破坏!
我的问题是,我如何使用字数,但只计算数字和字母?例如,在此代码中:
<div style="color:red;">hello</div>
我只需要计算 hello 字。那可能吗?
答案 0 :(得分:2)
尝试以下代码:只有当您知道字符串中包含哪些标记时,它才会起作用。
<?php
$tag_word = '<div style="color:red;">hello</div>';
// You can add any number of tags ex: strip_tags($tag_word, '<div><a><img><p>');
$word = strip_tags($tag_word, '<div>');
echo count($word);
?>