正则表达式按字符串解析mysql顺序

时间:2012-05-05 10:16:51

标签: php preg-replace

我可以拥有这些字符串:

  1. “volume asc,edition desc”
  2. “volume ASC,othercolumn desc,columnx DESC”
  3. 我想获得这样的数组:

    1. array('volume','asc','edition','desc');
    2. array('volume','asc','othercolumn','desc','columnx','desc');
    3. 如何使用regexp / preg_match函数实现这一目标?

3 个答案:

答案 0 :(得分:2)

您可以使用preg_split

$arr = array(
    "volume asc, edition desc",
    "volume ASC, othercolumn desc, columnx DESC"
);
foreach($arr as $str) {
    $res = preg_split('/[ ,]+/', $str);
    print_r($res);
}

<强>输出:

Array
(
    [0] => volume
    [1] => asc
    [2] => edition
    [3] => desc
)
Array
(
    [0] => volume
    [1] => ASC
    [2] => othercolumn
    [3] => desc
    [4] => columnx
    [5] => DESC
)

答案 1 :(得分:1)

使用诸如PHP SQL Parser之类的解析是一种更好的方法。

答案 2 :(得分:-1)

$str = 'volume asc, edition desc';
$chars = preg_split('/ /', $str);
print_r($chars);

Array ( [0] => volume [1] => asc, [2] => edition [3] => desc )

您可以使用strtolower函数将每个字符串转换为小写