基本上,我想將文本輸入到文本區域,然後使用它們。例如如何分割帶多個分隔符的列表?
variable1:[email protected]
variable1:[email protected]
variable1:[email protected]
我知道我可以使用爆炸,使每行到一個數組,然後使用foreach循環單獨使用的每一行,但我怎麼會分出三個變量來使用?
基本上,我想將文本輸入到文本區域,然後使用它們。例如如何分割帶多個分隔符的列表?
variable1:[email protected]
variable1:[email protected]
variable1:[email protected]
我知道我可以使用爆炸,使每行到一個數組,然後使用foreach循環單獨使用的每一行,但我怎麼會分出三個變量來使用?
嘗試使preg_split http://php.net/manual/en/function.preg-split.php
除了preg_split:
$line = 'variable11:[email protected]';
print_r(preg_split('/[:@]/', $line));
/*
Array
(
[0] => variable11
[1] => variable12
[2] => variable13
)
*/
你可以做一個preg_match_all:
$text = 'variable11:[email protected]
variable21:[email protected]
variable31:[email protected]';
preg_match_all('/([^\r\n:]+):([^\r\[email protected]]+)@(.*)\s*/', $text, $matches, PREG_SET_ORDER);
print_r($matches);
/*
Array
(
[0] => Array
(
[0] => variable11:[email protected]
[1] => variable11
[2] => variable12
[3] => variable13
)
[1] => Array
(
[0] => variable21:[email protected]
[1] => variable21
[2] => variable22
[3] => variable23
)
[2] => Array
(
[0] => variable31:[email protected]
[1] => variable31
[2] => variable32
[3] => variable33
)
)
*/
如果必要的話,你可以多次調用 「爆炸」
問題是'如何分裂',而不是'有多少種分裂方式'。 – 2010-06-16 08:10:40
多麼無用的評論。它是有效的解決方案。 – paullb 2010-06-16 08:23:23
@Andrejs,我同意@paullb在這裏:問題是如何獲得每行的標記/變量。迭代遍歷輸入行並調用'explode(...,explode(...))'是一個有效的解決方案,那麼爲什麼不建議呢?我可能不會使用「無用」這個詞,但它有一個相當負面的聲音。 – 2010-06-16 09:19:29