在PHP中剪切字符串

在PHP中剪切字符串,php,string,Php,String,我有一根绳子。有时超过50个字符,有时更短。如果它更长,我想将其剪切到50个字符(或最接近的“.”,如果可能的话,在50个字符之后 我目前使用strlen进行检查,然后使用strings数组将每个字符复制到一个新字符串中,直到达到50(在for循环中)。这似乎是一种不好的方法,而且速度很慢。到目前为止,我没有办法完成“.”部分 这是一种更好的剪断绳子的方法吗 一个人怎么能做“.”部分 您需要做的是从字符串中获取“子字符串” 在PHP中,函数是 要获得前5个字符 echo substr('abc

我有一根绳子。有时超过50个字符,有时更短。如果它更长,我想将其剪切到50个字符(或最接近的“.”,如果可能的话,在50个字符之后

我目前使用
strlen
进行检查,然后使用strings数组将每个字符复制到一个新字符串中,直到达到50(在for循环中)。这似乎是一种不好的方法,而且速度很慢。到目前为止,我没有办法完成“.”部分

  • 这是一种更好的剪断绳子的方法吗
  • 一个人怎么能做“.”部分

您需要做的是从字符串中获取“子字符串”

在PHP中,函数是

要获得前5个字符

echo substr('abcdef', 0, 5); //returns "abcde"

剩下的逻辑(接近“.”)留给您了。

您需要做的是从字符串中获取一个“子字符串”

在PHP中,函数是

要获得前5个字符

echo substr('abcdef', 0, 5); //returns "abcde"

剩下的逻辑(接近“.”)我留给你了。

试试这样的方法:

<?php

//somehow your $text string is set

if(strlen($text) > 50) {

    //this finds the position of the first period after 50 characters
    $period = strpos($text, '.', 50);

    //this gets the characters 0 to the period and stores it in $teaser
    $teaser = substr($text, 0, $period);

}

尝试以下方法:

<?php

//somehow your $text string is set

if(strlen($text) > 50) {

    //this finds the position of the first period after 50 characters
    $period = strpos($text, '.', 50);

    //this gets the characters 0 to the period and stores it in $teaser
    $teaser = substr($text, 0, $period);

}

类似的方法应该可以:

$string = substr($string, 0, 
    min(strpos($string, '.') >= 0? strpos($string, '.') : 50, 50));

类似的方法应该会奏效:

$string = substr($string, 0, 
    min(strpos($string, '.') >= 0? strpos($string, '.') : 50, 50));

首先使用
strpos
查找前50个字符后的“.”(如@ohmusama所说),但一定要检查返回值并使用
mb_strlen

$teaser = $text;
if (mb_strlen($text) > 50) {
   $period = strpos($text, '.', 50);
   if ($period !== false) {
      $teaser = substr($text, 0, $period);
   } else {
      // try finding a space...
      $space = strpos($text, ' ', 50);
      if ($space !== false) {
         $teaser = substr($text, 0, $space);
      } else {
         $teaser = substr($text, 0, 50);
      }
   }
}

首先使用
strpos
查找前50个字符后的“.”(如@ohmusama所说),但一定要检查返回值并使用
mb_strlen

$teaser = $text;
if (mb_strlen($text) > 50) {
   $period = strpos($text, '.', 50);
   if ($period !== false) {
      $teaser = substr($text, 0, $period);
   } else {
      // try finding a space...
      $space = strpos($text, ' ', 50);
      if ($space !== false) {
         $teaser = substr($text, 0, $space);
      } else {
         $teaser = substr($text, 0, 50);
      }
   }
}

仅此一项不能完成OP想要的“最近的”。你认为检查长度,然后再替换长的还是只替换所有内容更好?大约10%需要剪切…仅此一项不能完成“最近的”这是OP想要的。你认为最好检查长度,然后再替换长的还是只替换所有内容?大约10%需要剪切…很好的呼叫,我错过了。你是否也应该在下一个空格(“”)上打断如果可以的话?这将是创建摘要的最好效果。为什么mb_strlen比普通strlen好呢?这是关于unicode的…我们在一个项目中遇到了一个问题,当文本包含像“ä”这样的符号时,我们通过
strlen
确定字符串长度它计算了一个错误的长度。很好的呼叫,我错过了。你是否也应该在下一个空格(“”)上中断如果可以的话?这将是创建摘要的最好效果。为什么mb_strlen比普通strlen好呢?这是关于unicode的…我们在一个项目中遇到了一个问题,当文本包含像“ä”这样的符号时,我们通过
strlen
确定字符串长度它计算了一个错误的长度。已测试,已确认有效。我在$period位置检查中添加了+1,以将period包括在outputJust中再次测试,当字符串不包含“.”时似乎不起作用,所以我添加了if($period==0){$period=50;}或者尝试第二个代码集,因为它的功能更好,并且创建了一个更好的摘要。已测试,已确认有效。我在$period位置检查中添加了+1,以在outputJust中包含period。再次测试,当字符串不包含“.”时似乎不起作用,因此我添加了if($period==0){$period=50;}或者尝试第二个代码集,因为它的功能更好,并且创建了更好的摘要。