php - substr函数问题

时间:2013-12-07 12:18:14

标签: php substr

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

e.g。

$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 
*/

但根据我的需要,它必须显示直到“进入el”

请提前帮助我。

3 个答案:

答案 0 :(得分:2)

您可能需要使用strip_tags

echo substr(strip_tags($str2),0,300);

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

答案 1 :(得分:1)

这将解决问题,但它将删除您的超链接。你想保持超链接完整吗?

<?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 
*/

?>

答案 2 :(得分:0)

使用它

function limit_words($string, $word_limit){
    $words = explode(" ",$string);
    return implode(" ",array_splice($words,0,$word_limit));
}