使用PHP在字符串上提取一些值

时间:2014-09-24 19:47:48

标签: php string extract

我需要从字符串中提取值,其中我们将所有信息拆分为" |"或" ||"字符。这是两个例子:

word1 | "word2[word3]" || number1 | word4
word5 | "word6[word7]" || number2 | word8

这是字符串语法,我需要提取2个括号(word3和word7)中的值和||之后的值(number1和number2)。怎么做?

1 个答案:

答案 0 :(得分:0)

正如我在评论中提到的,我会使用preg_split,如:

$words = preg_split('/[\s\|"\[\]]+/', $string);

有关

$string = 'word1 | "word2[word3]" || number1 | word4
           word5 | "word6[word7]" || number2 | word8';

$ words数组将是:

Array
(
    [0] => word1
    [1] => word2
    [2] => word3
    [3] => number1
    [4] => word4
    [5] => word5
    [6] => word6
    [7] => word7
    [8] => number2
    [9] => word8
)