How do i start text str_split after some characters

点点圈 提交于 2019-12-14 03:28:17

问题


I have the code:

$txt = "lookSTARTfromhereSTARTagainhere";

$disp = str_split($txt, 4); 

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

which return 'look', 'STAR' 'Tfor' in a text line of 'lookSTARTfromhereSTARTagainhere' my problem is how do i start my text split from 'START' example my result output for text line of 'lookSTARTfromhereSTARTagainhere' after split look like 'from' 'here' 'again' thanks for your time and understanding


回答1:


it may not be possible by str_split as 'again' has 5 characters. you can get 'from', 'here' by following code.

$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:


how do i start my text split from 'START'

Simply with explode and array_slice functions:

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

print_r($result);

The output:

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



回答3:


If your expected output is four letter words from start you can explode on START then remove first item and use str_split to split each array item to four letter words.

$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




回答4:


<?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);

?>

output

Array ( [0] => from [1] => here [2] => again )



来源:https://stackoverflow.com/questions/47052931/how-do-i-start-text-str-split-after-some-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!