简体   繁体   中英

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

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 .

what is your variable $line value?

maybe you can var_dump($params) see its result, in regular expessions $params[o

preg_match_all("/\"([A-Za-z0-9\s]+)\"|([A-Za-z][A-Za-z0-9]*)/", '" "askdj" "asjdk" "asdasndw" "', $Matches))

因此,它将接受文字字符串和字符串变量。...@Zach Rattner的答案的大感谢。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM