[英]Split a string using regular expressions in PHP
I have this problem in out project 我在项目中遇到了这个问题
$line = substr($line,8);
$LIT_YARN = '".*"';
$VAR_NAME = '[A-Za-z][a-zA-Z0-9]*';
$TEMP2 = "/($LIT_YARN|$VAR_NAME)/";
$line = trim($line);
preg_match_all($TEMP2,$line,$params);
var_dump($params[0]);
the simple input is 简单的输入是
" "askdj" "asjdk" "asdasndw" "
the complicated input is 复杂的输入是
" "asd" Y "asd" X "
variables can also be inputted 也可以输入变量
simple input resulted to a single string 简单的输入导致单个字符串
params[0] => "askdj" "asjdk" "asdasndw"
I was expecting like this 我期待这样
params[0] => "askdj"
params[1] => "asjdk"
params[2] => "asdasndw"
complicated input should result to 复杂的输入应导致
params[0] => "asd"
params[1] => 'Y'
params[2] => "asd"
params[3] => 'X'
including the X and Y which are variables 包括X和Y这两个变量
how should i do the trick? 我应该怎么做?
This should do the trick: 这应该可以解决问题:
preg_match_all('/"([A-Za-z0-9]+)"/', '" "askdj" "asjdk" "asdasndw" "', $Matches))
You want to match a quote, then at least one alphanumeric character, then an ending quote. 您要匹配一个引号,然后至少匹配一个字母数字字符,再匹配一个结束引号。 Put the alphanumeric characters in parentheses to capture the output and put it in
$Matches
. 将字母数字字符放在括号中以捕获输出,并将其放在
$Matches
。
what is your variable $line value? 您的$ line变量值是多少?
maybe you can var_dump($params) see its result, in regular expessions $params[o 也许您可以在常规实验$ params [o中查看var_dump($ params)的结果
preg_match_all("/\"([A-Za-z0-9\s]+)\"|([A-Za-z][A-Za-z0-9]*)/", '" "askdj" "asjdk" "asdasndw" "', $Matches))
因此,它将接受文字字符串和字符串变量。...@Zach Rattner的答案的大感谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.