我有一个包含双引号或单引号的字符串.我需要做的是在引用之间回应所有内容:
$str = "'abc de', xye, jhy, jjou";
$str2 = "\"abc de\", xye, jhy, jjou";
Run Code Online (Sandbox Code Playgroud)
我不介意使用正则表达式(preg_match),或任何其他内置功能的PHP.
请指教.
问候,
$str = "'abc de', xye, \"jhy\", blah blah 'bob' \"gfofgok\", jjou";
preg_match_all('/".*?"|\'.*?\'/', $str, $matches);
print_r($matches);
Run Code Online (Sandbox Code Playgroud)
返回:
Array (
[0] => Array (
[0] => 'abc de'
[1] => "jhy"
[2] => 'bob'
[3] => "gfofgok"
)
)
Run Code Online (Sandbox Code Playgroud)
正则表达式的解释:
" -> Match a double quote
.* -> Match zero or more of any character
?" -> Match as a non-greedy match until the next double quote
| -> or
\' -> Match a single quote
.* -> Match zero or more of any character
?\' -> Match as non-greedy match until the next single quote.
Run Code Online (Sandbox Code Playgroud)
这$matches[0]是一个包含单引号或双引号内所有字符串的数组.