Php 当单词接近800时,在句号后换行
我有一个很长的描述,当字数大于800时,我想在每个句号后断开字符串 像计数800个字符,然后识别下一次出现的“.”一样,换行查找下一个800个单词,然后是句号,然后再次换行Php 当单词接近800时,在句号后换行,php,Php,我有一个很长的描述,当字数大于800时,我想在每个句号后断开字符串 像计数800个字符,然后识别下一次出现的“.”一样,换行查找下一个800个单词,然后是句号,然后再次换行 function TrimString($String, $Length){ if(strlen($String)<=$Length){ $stringValue=$String; } else { $new_l = strpos ($String , ".", $Len
function TrimString($String, $Length){
if(strlen($String)<=$Length){
$stringValue=$String;
} else {
$new_l = strpos ($String , ".", $Length);
$Length = $new_l+1;
$stringValue=substr($String,0,$Length);
}
return $stringValue;
}
提前感谢您的帮助。我理解您的问题如下:
在每800个符号后搜索第一个
,并插入一个新行
。
如果它是正确的,那么您必须在方法中循环使用字符串,如本例所示
function trimString($string, $length){
$str_length = strlen($string);
if ($str_length <= $length){
return $string;
}
$from = 0;
$string_value = '';
// max amount of loops based on the length of $string and $length
$loops = ceil($str_length / $length);
for ($i = 1; $i <= $loops; $i++) {
// $from could be 0 or the length of your last finding dot
$tmp_length = $from + $length;
if ($tmp_length > $str_length) {
break;
}
// get the position of the dot after the calculated length
$pos = strpos($string , ".", $tmp_length);
// append the dot and a br-tag for a new line
$string_value .= substr(substr($string, 0, $pos), $from) . ".<br/>";
// set to the next position after the finding dot
$from = $pos + 1;
}
return $string_value;
}
echo trimString("This is a test. This is not a test. This is a new test.", 5);
我理解你的问题,比如:
在每800个符号后搜索第一个
,并插入一个新行
。
如果它是正确的,那么您必须在方法中循环使用字符串,如本例所示
function trimString($string, $length){
$str_length = strlen($string);
if ($str_length <= $length){
return $string;
}
$from = 0;
$string_value = '';
// max amount of loops based on the length of $string and $length
$loops = ceil($str_length / $length);
for ($i = 1; $i <= $loops; $i++) {
// $from could be 0 or the length of your last finding dot
$tmp_length = $from + $length;
if ($tmp_length > $str_length) {
break;
}
// get the position of the dot after the calculated length
$pos = strpos($string , ".", $tmp_length);
// append the dot and a br-tag for a new line
$string_value .= substr(substr($string, 0, $pos), $from) . ".<br/>";
// set to the next position after the finding dot
$from = $pos + 1;
}
return $string_value;
}
echo trimString("This is a test. This is not a test. This is a new test.", 5);
还有什么在这里不起作用?(这似乎对我有用:-)我没有看到任何换行符序列/字符?我的字符串有100万个单词,我想在下一次出现句号后,每800个单词后就换行一次。然后寻找下一个800字,当找到下一个句号时暂停:)。这里有什么不起作用?(这似乎对我有用:-)我没有看到任何换行符序列/字符?我的字符串有100万个单词,我想在下一次出现句号后,每800个单词后就换行一次。然后寻找下一个800字,在找到下一个句号时暂停:)。谢谢我的朋友,我认为它正在按照我第一眼看到的预期工作。是的,没问题,如果这是你问题的答案,那么就这样标记@phpdeva在这方面遇到了问题,如果最后剩下的字符少于800,那么它将跳过该部分。Ex a字符串有1800个单词,显示1600个,跳过剩余的200个单词,需要您的帮助。我尝试了并对函数进行了一些更改,它按预期工作。我尝试获取地板的值,然后运行剩余的字符串。我的逻辑正确吗@UfguFugulluI已经对这个函数进行了更新,它将注意到最后一次出现的
$search
@phpdev谢谢我的朋友,我认为它正在按照我第一眼看到的预期工作。是的,没有问题,如果这是你问题的答案,那么就把它标记为@phpdeva在这方面遇到了问题,如果最后剩下的字符少于800,则跳过该部分。Ex a字符串有1800个单词,显示1600个,跳过剩余的200个单词,需要您的帮助。我尝试了并对函数进行了一些更改,它按预期工作。我尝试获取地板的值,然后运行剩余的字符串。我的逻辑正确吗@UfguFugulluI对该函数进行了更新,它将注意到上次出现的$search
@phpdev
function trimString($haystack, $length, $search, $insert = ''){
$str_length = strlen($haystack);
if ($str_length <= $length){
return $haystack;
}
$from = 0;
$string_value = '';
$search_after = $length;
// find the last position of $search in the string
$pos_last_dot = strrpos($haystack, $search);
while ($search_after <= $pos_last_dot) {
// get the position of $search after the last position
$pos = strpos($haystack , $search, $search_after);
// append the $search and the $insert for a new line
$string_value .= substr(substr($haystack, 0, $pos), $from) . $search . $insert;
// set the cursor to the next position after the finding $search
$from = $pos + 1;
// add the last occurence of $search
if (($from + $length) >= $pos_last_dot && $pos != $pos_last_dot) {
$string_value .= substr(substr($haystack, 0, $pos_last_dot), $from) . $search . $insert;
break;
}
$search_after = $from + $length;
}
return $string_value;
}
echo trimString("This is a test. This is not a test. This is a new test This is a new test. foo. bar.", 10, ".", "<br/>");