【发布时间】:2013-09-12 06:10:11
【问题描述】:
我想用多个分隔符分解一个字符串。我已经用这个 PHP 函数 eplode 了:
function multiexplode ($delimiters,$string) {
$ready = str_replace($delimiters, $delimiters[0], $string);
$launch = explode($delimiters[0], $ready);
return $launch;
}
$text = "here is a sample: this text, and this will be exploded. this also | this one too :)";
$exploded = multiexplode(array(",",".","|",":"),$text);
这个输出是:
Array (
[0] => here is a sample
[1] => this text
[2] => and this will be exploded
[3] => this also
[4] => this one too
[5] => )
)
我可以使用以下多个分隔符来内爆这个数组吗:,.|:?
编辑:
为了定义规则,我认为这是最好的选择:
$test = array(':', ',', '.', '|', ':');
$i = 0;
foreach ($exploded as $value) {
$exploded[$i] .= $test[$i];
$i++;
}
$test2 = implode($exploded);
$test2 的输出为:
here is a sample: this text, and this will be exploded. this also | this one too :)
我现在只需要知道如何定义 $test 数组(可能与 preg_match()?),以便它匹配这些值 , . | : 并按顺序设置变量它在字符串中出现的地方变成了一个数组。这可能吗?
【问题讨论】:
-
您需要定义如何应用分隔符的规则。
-
对于从explode 得到的数组,如果您想使用4 个字符(
,.|和:)进行内爆,您希望结果如何? -
@mario 您建议的解决方案应该是什么样的?恕我直言,删除多个分隔符很容易 - 如图所示 - 但不可能制作方法
multiimplode() -
@hek2mgl 没错,我完全忽略了 OP 的含义。这显然不是一个可逆的过程。
-
@mario OK.. 如果你能给我一个我会吃熊的方法。 (或类似的东西;)
标签: php arrays explode delimiter implode