(PHP)如何将字符串拆分为以逗号分隔的多个字符串,
就像我有一个字符串"Hello my neighbor"
,
然后在拆分过程之后,它应该是
("Hello my neighbor","Hello my","Hello neighbor","my neighbor","Hello","my","neighbor")
答案 0 :(得分:0)
试试这个:
<?php
$string = "Hello my neighbor";
$str = explode(" ",$string);
$newArray = [];
$newArray[] = $string;
for($i = 0; $i<count($str); $i++) {
if($i <= 1) {
$newArray[] = $str[$i]." ".$str[$i+1];
if($i != 0) {
$newArray[] = $str[$i-1]." ".$str[$i+1];
}
}
$newArray[] = $str[$i];
}
echo "<pre>";
print_r($newArray);
echo "</pre>";