首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >php - substr函数问题

问php - substr函数问题
EN

Stack Overflow用户
提问于 2013-12-07 20:18:14
回答 3查看 427关注 0票数 1

当我们使用substr来显示字符串的一部分,但如果我们的字符串有锚标记,那么显示的字符串内容比其他字符串要小

例如:

代码语言:javascript
复制
$str1="is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It";

$str2="is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy <a href='http://www.google.com'>text</a> ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It";

$str1Count=strlen($str1); // 357

$str2Count=strlen($str2); // 393

if($str1Count > 300){
   echo substr($str1,0,300)."<br/><br/>";
}

/* 
output:
is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into el
*/


if($str2Count > 300){
    echo substr($str2,0,300);
}

/* 
output:
is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy <a href='http://www.google.com'>text</a> ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five 
*/

但根据我的需要,它必须显示到"into el“为止

请帮我提前谢谢。

EN

回答 3

Stack Overflow用户

发布于 2013-12-07 20:36:25

您可能需要使用strip_tags

代码语言:javascript
复制
echo substr(strip_tags($str2),0,300);

注意:在检查字符串的长度时,也不要忘记使用strip_tags。

票数 2
EN

Stack Overflow用户

发布于 2013-12-07 21:39:39

这将完成此操作,但它将删除您的超链接。是否要保持超链接完好无损?

代码语言:javascript
复制
<?php   

$str1="is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It";

$str2="is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy <a href='http://www.google.com'>text</a> ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It";

$str1Count=strlen(strip_tags($str1)); // 357

$str2Count=strlen(strip_tags($str2)); // 393

if($str1Count > 300){
   echo substr(strip_tags($str1),0,300)."<br/><br/>";
}

/* 
output:
is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into el
*/


if($str2Count > 300){
    echo substr(strip_tags($str2),0,300);
}

/* 
output:
is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy <a href='http://www.google.com'>text</a> ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five 
*/

?>
票数 1
EN

Stack Overflow用户

发布于 2013-12-07 20:26:55

使用它

代码语言:javascript
复制
function limit_words($string, $word_limit){
    $words = explode(" ",$string);
    return implode(" ",array_splice($words,0,$word_limit));
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20441274

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档