如何删除characters_to_eliminate
循环中每个字符串的第一个字符foreach
?
编辑:characters_to_eliminate
是3个随机字符,它们并不总是相同。
在第6行,我尝试:
$val = substr($val, 3);
但只删除数组中第一个值的前3个字符。
function multiexplode ($delimiters,$string) {
$ary = explode($delimiters[0],$string);
array_shift($delimiters);
if($delimiters != NULL) {
foreach($ary as $key => $val) {
$ary[$key] = multiexplode($delimiters, $val);
}
}
return $ary;
}
// Example of use
$string = "characters_to_eliminate abc, characters_to_eliminate def: characters_to_eliminate ghi, characters_to_eliminate jkl";
$delimiters = Array(",",":");
$res = multiexplode($delimiters,$string);
echo '<pre>';
print_r($res);
echo '</pre>';
答案 0 :(得分:0)
尝试foreach($ary as $key => &$val)
为了能够使用
$value
在&
之前的循环中直接修改数组元素。在这种情况下,该值将通过引用分配。 (通过http://php.net/manual/en/control-structures.foreach.php)
答案 1 :(得分:0)
$string = str_replace('characters_to_eliminate ', '', $string); // abc, def: ghi, jkl