Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将文本字段拆分为125个字符后的两部分 我需要把文本分割成两个部分,这个函数可以在一个单词的中间切掉,我需要它来计算单词的开头或结尾,给出或取几个字符。 $str = "Lorem Ipsum 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 was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum" $first125 = substr($str, 0, 125); $theRest = substr($str, 125);_Php_Arrays_String - Fatal编程技术网

Php 将文本字段拆分为125个字符后的两部分 我需要把文本分割成两个部分,这个函数可以在一个单词的中间切掉,我需要它来计算单词的开头或结尾,给出或取几个字符。 $str = "Lorem Ipsum 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 was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum" $first125 = substr($str, 0, 125); $theRest = substr($str, 125);

Php 将文本字段拆分为125个字符后的两部分 我需要把文本分割成两个部分,这个函数可以在一个单词的中间切掉,我需要它来计算单词的开头或结尾,给出或取几个字符。 $str = "Lorem Ipsum 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 was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum" $first125 = substr($str, 0, 125); $theRest = substr($str, 125);,php,arrays,string,Php,Arrays,String,我不能基于字数,因为我需要的字符范围不超过130个字符 $str = "Lorem Ipsum 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 scr

我不能基于字数,因为我需要的字符范围不超过130个字符

$str = "Lorem Ipsum 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 was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum"

$first125 = substr($str, 0, 125);
$theRest = substr($str, 125);

谢谢

我不确定我脑子里是否有一个本机php函数,但我认为这应该适合你。当然,您需要添加一些内容来检查是否至少有125个字符,以及在假定的第125个字符后面是否有空格

    $k = 'a';
    $i = 0;
    while($k != ' '):
       $k = substr($str, (125+$i), 1);
       if($k == ' '):
         $first125 = substr($str, 0, (125+$i));
         $theRest = substr($str, (125+$i+1));
       else:
             $i++;
       endif;
    endwhile;

我不确定我脑子里是否有一个本机php函数,但我认为这应该适合你。当然,您需要添加一些内容来检查是否至少有125个字符,以及在假定的第125个字符后面是否有空格

    $k = 'a';
    $i = 0;
    while($k != ' '):
       $k = substr($str, (125+$i), 1);
       if($k == ' '):
         $first125 = substr($str, 0, (125+$i));
         $theRest = substr($str, (125+$i+1));
       else:
             $i++;
       endif;
    endwhile;

这里有一个非常基本的解决方案,可以帮助你开始工作

$first125 = substr($str, 0, 125);
$theRest = substr($str, 125);

$remainder_pieces = explode(" ", $theRest, 2);

$first125 .= $remainder_pieces[0];
$theRest = $remainder_pieces[1];

echo $first125."</br>";
echo $theRest;
$first125=substr($str,0125);
$theRest=substr($str,125);
$restains\u parties=爆炸(“,$theRest,2);
$first125.=$FRISTS_件[0];
$theRest=$restricts\u件[1];
echo$first125.“
”; echo$theRest;

<>但是有更多的事情要考虑,例如用这个解决方案,如果第一百二十五个字符是单词结束后的一个空格,它将包括另一个单词,因此,您可能需要添加一些额外的检查方法,以尝试使其尽可能准确。

以下是一个非常基本的解决方案,可以帮助您开始进行检查

$first125 = substr($str, 0, 125);
$theRest = substr($str, 125);

$remainder_pieces = explode(" ", $theRest, 2);

$first125 .= $remainder_pieces[0];
$theRest = $remainder_pieces[1];

echo $first125."</br>";
echo $theRest;
$first125=substr($str,0125);
$theRest=substr($str,125);
$restains\u parties=爆炸(“,$theRest,2);
$first125.=$FRISTS_件[0];
$theRest=$restricts\u件[1];
echo$first125.“
”; echo$theRest;

但是有更多的事情要考虑,例如,用这个解决方案,如果第一百二十五个字符是单词结束后的一个空格,它将包括另一个单词,因此,你可能需要添加一些额外的检查方法来尝试使它尽可能精确。

< P>:

<?php
$str = "Lorem Ipsum 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 was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum";
$str = explode(' ', $str);
$substr = "";

foreach ($str as $cur) {
    if (strlen($substr) >= 125) {
        break;
    }
    $substr .= $cur . ' ';
}
echo $substr;
?>
试试: