使用 PHP substr,如何找到前面的空格而不是后面的空格?
With PHP substr, how can I find the previous whitespace instead of the following whitespace?
我想在被截断的单词之前的空白处截断一个字符串。目前我有工作代码用于在被截断的单词 之后 处截断一个字符串,但这超出了我的最大字符限制。
我做了一些实验,我找到了找到空白的解决方案,这样 strpos 就不会在单词的中间裁剪掉,而是会在字符限制内的单词之后找到空白,并且然后裁剪字符串。
$thestring = "my really really really really really really really";
$getlength = strlen($thestring);
$maxLength = 42;
if ($getlength > $maxLength) {
echo substr($thestring, 0, strpos($thestring, ' ', $maxLength));
echo "...";
} else {
echo $thestring;
}
echo " - long string";
这是完全可行的,唯一的问题是我想对字符串中的字符数进行硬性限制。但是这样做是在它介于两者之间的单词之后进行裁剪,这使得它超出了限制的几个字符。
例如,这将 return 这个字符串:
my really really really really really really - long string
因为 42 个字符落在最后 "really",所以它将长度扩展到下一个空格。但是现在 returned 字符串是 44 个字符。
我更希望它做的是,因为确切的 42 个字符 returns:
my really really really really really real
然后,我想将其裁剪到 prior 空格,而不是后面的空格。所以就像在这个例子中,我想要这个 returned:
my really really really really really - long string
这很好,因为它裁剪掉了最后一个单词的一部分,所以 return 只是最后一个单词的一部分 real
看起来很糟糕,或者 after 整个单词 really
超出了我的最大字符限制,然后它裁剪了部分最后一个单词,导致总字符串低于最大限制,这就是我想要的。
所以就像我想要的 returned 上面的字符串是 37 个字符,这正是我想要的,因为它少于 42 个。
那么,我如何修改我的代码以将其裁剪到 maxLength 末尾所在的词的前面的空白处,而不是后面的空白处?
你必须使用 strpos 和 substr 试试这个代码:
$content = "my really really really really really really really";
$pos=strpos($content, ' ', 20);
$new = substr($content,0,$pos );
$fromfirstwhitespace = preg_replace('/\W\w+\s*(\W*)$/', '', $new);
echo ($fromfirstwhitespace);
干杯!!!
不是很好的制作方法,但至少它可以工作。
- 你有你的 $strlen = 42
- 将此字符串切片以匹配 $strlen
- 按space
分解新字符串
- 打印除最后一个以外的所有数组值。
编辑:
这是代码,实际上我对此不是很满意,但它至少可以工作。
$str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
$strLength = 42;
$strCut = substr($str, 0, $strLength);
$strExp = explode(" ", $strCut);
$newTxtSize = count($strExp) - 2;
$output = "";
for($i = 0; $i <= $newTxtSize; $i++){
$output .= $strExp[$i] .' ';
}
echo rtrim($output, " ") . "...";
好的。我不知道为什么我的代码示例对您不起作用(这真的很奇怪):-/
但这是基于您的来源的完整代码:
<?php
$thestring = "my really really really really really really really";
$getlength = strlen($thestring);
$maxLength = 42;
if ($getlength > $maxLength) {
echo substr($thestring, 0, strrpos($thestring, ' ', $maxLength-$getlength));
echo "...";
} else {
echo $thestring;
}
echo " - long string";
对于我的控制台,它输出:
[64] $ php ./ex.php
my really really really really really... - long string
一个默认剪切的短函数,不剪切,或者剪切到第一个 space 在想要的字符数之后找到的下一个 ...
function cut_string($nb_chars, $str){
// define a nb chart by defaut
$nb_chars_default = 250;
// use default or number wanted
$nb_chars = ( $nb_chars != 0
|| $nb_chars != '' ) ? $nb_chars : $nb_chars_default;
// size of the string in nb chars
$len_str = strlen($str);
// if the string is too long
if( $len_str > $nb_chars ){
// cut string just at the first space after nb chars wanted
return substr( $str, 0, strrpos($str, ' ', $nb_chars-$len_str) );
}
else{
return $str;
}
}
// use :
$str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum";
$my_cut_string = cut_string(200, $str);
echo $my_cut_string;
// output :
// Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
我想在被截断的单词之前的空白处截断一个字符串。目前我有工作代码用于在被截断的单词 之后 处截断一个字符串,但这超出了我的最大字符限制。
我做了一些实验,我找到了找到空白的解决方案,这样 strpos 就不会在单词的中间裁剪掉,而是会在字符限制内的单词之后找到空白,并且然后裁剪字符串。
$thestring = "my really really really really really really really";
$getlength = strlen($thestring);
$maxLength = 42;
if ($getlength > $maxLength) {
echo substr($thestring, 0, strpos($thestring, ' ', $maxLength));
echo "...";
} else {
echo $thestring;
}
echo " - long string";
这是完全可行的,唯一的问题是我想对字符串中的字符数进行硬性限制。但是这样做是在它介于两者之间的单词之后进行裁剪,这使得它超出了限制的几个字符。
例如,这将 return 这个字符串:
my really really really really really really - long string
因为 42 个字符落在最后 "really",所以它将长度扩展到下一个空格。但是现在 returned 字符串是 44 个字符。
我更希望它做的是,因为确切的 42 个字符 returns:
my really really really really really real
然后,我想将其裁剪到 prior 空格,而不是后面的空格。所以就像在这个例子中,我想要这个 returned:
my really really really really really - long string
这很好,因为它裁剪掉了最后一个单词的一部分,所以 return 只是最后一个单词的一部分 real
看起来很糟糕,或者 after 整个单词 really
超出了我的最大字符限制,然后它裁剪了部分最后一个单词,导致总字符串低于最大限制,这就是我想要的。
所以就像我想要的 returned 上面的字符串是 37 个字符,这正是我想要的,因为它少于 42 个。
那么,我如何修改我的代码以将其裁剪到 maxLength 末尾所在的词的前面的空白处,而不是后面的空白处?
你必须使用 strpos 和 substr 试试这个代码:
$content = "my really really really really really really really";
$pos=strpos($content, ' ', 20);
$new = substr($content,0,$pos );
$fromfirstwhitespace = preg_replace('/\W\w+\s*(\W*)$/', '', $new);
echo ($fromfirstwhitespace);
干杯!!!
不是很好的制作方法,但至少它可以工作。
- 你有你的 $strlen = 42
- 将此字符串切片以匹配 $strlen
- 按space 分解新字符串
- 打印除最后一个以外的所有数组值。
编辑:
这是代码,实际上我对此不是很满意,但它至少可以工作。
$str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
$strLength = 42;
$strCut = substr($str, 0, $strLength);
$strExp = explode(" ", $strCut);
$newTxtSize = count($strExp) - 2;
$output = "";
for($i = 0; $i <= $newTxtSize; $i++){
$output .= $strExp[$i] .' ';
}
echo rtrim($output, " ") . "...";
好的。我不知道为什么我的代码示例对您不起作用(这真的很奇怪):-/
但这是基于您的来源的完整代码:
<?php
$thestring = "my really really really really really really really";
$getlength = strlen($thestring);
$maxLength = 42;
if ($getlength > $maxLength) {
echo substr($thestring, 0, strrpos($thestring, ' ', $maxLength-$getlength));
echo "...";
} else {
echo $thestring;
}
echo " - long string";
对于我的控制台,它输出:
[64] $ php ./ex.php
my really really really really really... - long string
一个默认剪切的短函数,不剪切,或者剪切到第一个 space 在想要的字符数之后找到的下一个 ...
function cut_string($nb_chars, $str){
// define a nb chart by defaut
$nb_chars_default = 250;
// use default or number wanted
$nb_chars = ( $nb_chars != 0
|| $nb_chars != '' ) ? $nb_chars : $nb_chars_default;
// size of the string in nb chars
$len_str = strlen($str);
// if the string is too long
if( $len_str > $nb_chars ){
// cut string just at the first space after nb chars wanted
return substr( $str, 0, strrpos($str, ' ', $nb_chars-$len_str) );
}
else{
return $str;
}
}
// use :
$str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum";
$my_cut_string = cut_string(200, $str);
echo $my_cut_string;
// output :
// Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut