如何在一些字符后启动文本str_split

时间:2017-11-01 10:38:04

标签: php string split character

我有代码:

$txt = "lookSTARTfromhereSTARTagainhere";

$disp = str_split($txt, 4); 

for ($b = 0; $b<3; $b++) {
    echo "$disp[$b]"; 
} 

返回&#39; look&#39;,&#39; STAR&#39; &#39; Tfor&#39;在&{39; lookSTARTfromhereSTARTagainhere&#39;的文本行中我的问题是如何开始从&#39; START&#39;例如,&#39; lookSTARTfromhereSTARTagainhere&#39;的文本行的结果输出分手后看起来像from&#39; &#39; here&#39; &#39; again&#39;感谢您的时间和理解

4 个答案:

答案 0 :(得分:0)

  

如何从&#39; START&#39;

开始分割文字

只需使用explodearray_slice函数:

$txt = "lookSTARTfromhereSTARTagainhere";
$result = array_slice(explode('START', $txt), 1);

print_r($result);

输出:

Array
(
    [0] => fromhere
    [1] => againhere
)

答案 1 :(得分:0)

str_split可能无法再次使用&#39;有5个字符。你可以从&#39;,&#39;这里&#39;通过以下代码。

$txt = "lookSTARTfromhereSTARTagainhere";
$txt = str_replace('look','',$txt);
$txt = str_replace('START','',$txt);
$disp = str_split($txt, 4); 
for ($b = 0; $b<3; $b++) {
    echo "$disp[$b]"; 
} 

答案 2 :(得分:0)

如果您的预期输出是从开始的四个字母单词,您可以在START上爆炸然后删除第一个项目并使用str_split将每个数组项目拆分为四个字母单词。

$txt = "lookSTARTfromhereSTARTagainhere";

$arr = explode("START", $txt); // Explode on START
unset($arr[0]); // first item is before START, we don't need that.
$res = [];
foreach($arr as $val){
    $temp = str_split($val, 4); // Split array item on four letters.
    $res = array_merge($res, $temp); // merge the new array with result array
}
var_dump($res);

https://3v4l.org/3BQ1b

答案 3 :(得分:0)

<?php

$txt = "lookSTARTfromhereSTARTagainhere";

$split = explode("START",$txt);
unset($split[0]);

$first_str = str_split($split[1],4);
$t2 = str_split($split[2],5);

$second_str = $t2[0];

array_push($first_str,$second_str);

print_r($first_str);

?>

输出

  

数组([0] =&gt;来自[1] =&gt;这里[2] =&gt;再次)