使用正则表达式检查字符串的某些值

时间:2014-12-06 18:40:49

标签: php regex preg-match

如何使用preg_match搜索变量$message以查找文字[sh 2000]

变量$ message是从Web服务发送的。

我想知道[sh 2000]是否已提交为[sh 2000]所以我想忽略sh2000之间存在空格。 数组[0]中的AND [PUT SH和数组[1]中的#NUMBBER;

之前([ONLYNUMBER] LIKE [2])我用过:

preg_match("/\[(\p{N}*)\]/", $message, $find)

任何帮助? 感谢

1 个答案:

答案 0 :(得分:0)

$ php -r '$message = "[sh 2000]"; preg_match("/\[(\w+)\s*(\p{N}+)\]/", $message, $find); print_r($find);'
Array
(
    [0] => [sh 2000]
    [1] => sh
    [2] => 2000
)

或:

$ php -r '$message = "[sh 2000]"; preg_match("/\[(\w+)\s*(\p{N}+)\]/", $message, $find); unset($find[0]); print_r($find);'
Array
(
    [1] => sh
    [2] => 2000
)