php-substr函数问题

php-substr函数问题,php,substr,Php,Substr,当我们使用substr显示字符串的一部分时,但如果我们的字符串具有锚定标记,则显示的字符串内容比其他字符串小 e、 g $str1=只是印刷和排版行业的虚拟文本。自16世纪以来,Lorem Ipsum一直是行业标准的虚拟文本,当时一位不知名的印刷商拿起一个打印工具,将其拼凑成一本打印样本书。它不仅存活了五个世纪,而且还跨越到电子排版,基本上保持不变。它”; $str2=“只是印刷和排版行业的虚拟文本。自16世纪以来,Lorem Ipsum一直是业界的标准模仿者,当时一位不知名的印刷商拿着一个印刷

当我们使用substr显示字符串的一部分时,但如果我们的字符串具有锚定标记,则显示的字符串内容比其他字符串小

e、 g

$str1=只是印刷和排版行业的虚拟文本。自16世纪以来,Lorem Ipsum一直是行业标准的虚拟文本,当时一位不知名的印刷商拿起一个打印工具,将其拼凑成一本打印样本书。它不仅存活了五个世纪,而且还跨越到电子排版,基本上保持不变。它”;
$str2=“只是印刷和排版行业的虚拟文本。自16世纪以来,Lorem Ipsum一直是业界的标准模仿者,当时一位不知名的印刷商拿着一个印刷机,将其拼凑成一本印刷样书。它不仅存活了五个世纪,而且还跨越到电子排版,基本上保持不变。它”;
$str1Count=strlen($str1);//357
$str2Count=strlen($str2);//393
如果($str1计数>300){
echo substr($str1,0300)。“

”; } /* 输出: 只是印刷和排版行业的虚拟文本。自16世纪以来,Lorem Ipsum一直是印刷行业的标准虚拟文本,当时一位不知名的印刷商拿着一个铅字槽,抢着制作一本铅字样本书。它不仅存活了五个世纪,而且还跨越了el */ 如果($str2计数>300){ echo substr($2,0300); } /* 输出: 只是印刷和排版行业的虚拟文本。从16世纪开始,Lorem Ipsum就一直是印刷行业的标准虚拟文本,当时一位不知名的印刷商拿起一个印刷工具,将其拼凑成一本字体样本书。它不仅存活了五年 */
但根据我的需要,它必须显示到“进入el”

请先帮我谢谢。

使用它

function limit_words($string, $word_limit){
    $words = explode(" ",$string);
    return implode(" ",array_splice($words,0,$word_limit));
}
您可能需要使用


注意:在检查字符串长度时,不要忘记使用
strip\u标记。

这会起作用,但会删除超链接。是否要保持超链接完整

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

?>


说真的,这些是你能想到的最短的字符串示例?:/20个字符怎么样?你是否尝试过
回显htmlspecialchars(substr($str2,0300));
(如果你在浏览器中显示结果)?htmlspecialchars()直到“只有五个”才显示“为什么要用这个?”??OP不想拆分单词。@bansi这将给出正确的结果,并且(字符串具有锚定标记,然后显示的字符串内容比其他字符串小)不会出现问题
echo substr(strip_tags($str2),0,300);
<?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 
*/

?>