问题
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