-1

如何删除循环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>';
4

2 回答 2

1

尝试foreach($ary as $key => &$val)

为了能够直接修改循环内的数组元素,$value&. 在这种情况下,该值将通过引用分配。(通过http://php.net/manual/en/control-structures.foreach.php

于 2013-03-08T19:39:26.607 回答
0

str_replace()

$string = str_replace('characters_to_eliminate ', '', $string); // abc, def: ghi, jkl
于 2013-03-08T19:40:43.720 回答