Php 修剪一根线并在其末端添加
我想做的基本上是缩短一段文字,如果文字太长,在末尾加上“…”。然而(这里是它变得毛茸茸的地方)我只想应用这个,如果字符串的最后一个字符是空格。如果不是空格,那么我想遍历字符串(Backwords),直到找到一个空格,然后当找到空格时,我会在其中添加“…” 原因是我不想在一个词还没说完的时候加上“…”,比如“wor…”我宁愿它是“word…” 以下是我迄今为止尝试过的内容(注意:$row\u object\u title只是一个来自数据库的字符串,因此可以安全地假设它只是一个随机文本): 谢谢你的帮助 更新:Php 修剪一根线并在其末端添加,php,string,str-replace,substr,Php,String,Str Replace,Substr,我想做的基本上是缩短一段文字,如果文字太长,在末尾加上“…”。然而(这里是它变得毛茸茸的地方)我只想应用这个,如果字符串的最后一个字符是空格。如果不是空格,那么我想遍历字符串(Backwords),直到找到一个空格,然后当找到空格时,我会在其中添加“…” 原因是我不想在一个词还没说完的时候加上“…”,比如“wor…”我宁愿它是“word…” 以下是我迄今为止尝试过的内容(注意:$row\u object\u title只是一个来自数据库的字符串,因此可以安全地假设它只是一个随机文本): 谢谢你的
if (strlen($row_object_title) > 50){
// check for spaces
$short = substr($row_object_title,50);
$chr = substr($short,-1);
$ascii = ord($chr);
if ($ascii == "32"){
//$trim = "...";
//$final = str_replace($short,$trim,$row_object_title);
$row_object_title .= "...";
}else{
$x = 49;
while($ascii != "32"){
$chr = substr($short,$x,1);
$ascii = ord($chr);
if ($ascii == "32"){
// we got a space!
$row_object_title .= "...";
}
$x = $x - 1;
}
}
}
第二次更新:
每个人都在发布他们版本的脚本(非常感谢),但你们能尝试修复我的脚本而不是发布其他方式吗?ord()
返回一个整数,但您可以使用检查$ascii
的值==编码>并将其与字符串进行比较。与相反=代码>,==代码>仅当操作数的类型也匹配时才为真
此外,要从字符串中提取单个字符,请指定子字符串的长度:
$chr = substr($short,$x,1);
您也可以直接为字符串编制索引:$chr=$short{$x}
编辑:发现另外两个缺陷:
第二行应该是:
$short = substr($row_object_title, 0, 50);
(您错过了长度参数之前的偏移。)
此外,您正在将“…”
附加到原始字符串,而不是缩短的版本。下面是(希望)已更正脚本的完整版本
if (strlen($row_object_title) > 50){
// check for spaces
$short = substr($row_object_title,50);
$chr = substr($short,-1);
$ascii = ord($chr);
if ($ascii == 32){
$row_object_title = $short . "...";
}else{
$x = 49;
while($ascii != 32){
$chr = substr($short,$x,1);
$ascii = ord($chr);
if ($ascii == 32){
// we got a space!
$row_object_title = substr($short, 0, $x) . "...";
}
$x = $x - 1;
}
}
}
我使用以下函数,不记得在哪里找到它。清除文本中的HTML,并将字符串限制为$limit
个字符,而不中断单词
function show_chopped_text($str, $limit)
{
$regex = "/<\/?\w+((\s+\w+(\s*=\s*(?:\".*?\"|'.*?'|[^'\">\s]+))?)+\s*|\s*)\/?>/i";
$strip_html = preg_replace($regex,'',$str);
$rough_short_par = substr($strip_html, 0, $limit);
$last_space_pos = strrpos($rough_short_par, " ");
$clean_short_par = substr($rough_short_par, 0, $last_space_pos);
$clean_sentence = $clean_short_par . "...";
return $clean_sentence;
}
函数显示切碎的文本($str,$limit)
{
$regex=“/如果我正确理解您的问题,这应该可以做到:
// if string does *not* end with one or more whitespace characters
if(!preg_match('/\s+$/',$string)) {
// replace everything after last group of one or more whitespaces with '...'
$string = preg_replace('/(.*)\s+.*/','\1...',$string);
}
$text=“这应该正好合适!但如果我们把它加长,它就不会”;
如果(strlen($text)>50){
$text=substr($text,0,50);
对于($i=strlen($text)-1;$i>0;$i--){
如果($text[$i]==''| |$text[$i]==','| |$text[$i]=='。| |$text[$i]=='!'| |$text[$i]=='?')){
$text=substr($text,0,$i)。“…阅读更多”;
打破
}
}
}
这项工作适合我(应该添加正则表达式,但我不太擅长这些…。您好,先生,请滚动到我的问题,在你告诉我的所有内容下面,你会发现我的代码中有更新,但是仍然不起作用,对我做错了什么有什么想法吗?好的,我发现了更多的缺陷(见上面的编辑)。不起作用。不确定为什么
// if string does *not* end with one or more whitespace characters
if(!preg_match('/\s+$/',$string)) {
// replace everything after last group of one or more whitespaces with '...'
$string = preg_replace('/(.*)\s+.*/','\1...',$string);
}
$text = "This should just fit in! But if we make it longer, it won't ";
if (strlen($text) > 50) {
$text = substr($text, 0, 50);
for ($i = strlen($text) - 1; $i > 0; $i--) {
if ($text[$i] === ' ' || $text[$i] === ',' || $text[$i] === '.' || $text[$i] === '!' || $text[$i] === '?') {
$text = substr($text, 0, $i) . '... <b>Read More</b>';
break;
}
}
}