我的字符串格式如下: -
"{\"A5\";\"A6\";\"A7\";\"varying number of params...\"}"
如何使用PHP将字符串转换为
A5, A6, A7, varying number of params...
我知道str_replace
是一种方式,但我想知道用正则表达式做得更好吗?
答案 0 :(得分:2)
如果您不需要正则表达式的强大功能,也可以将str_replace用于数组:
echo str_replace(array('"{\"', '\";\"', '\"}"'), array("", ", ", ""), $str);
- > A5, A6, A7, varying number of params...
test at eval.in
答案 1 :(得分:1)
(?<=\\")[^\\;]+
试试这个。看看演示。
https://regex101.com/r/sH8aR8/53
$re = "/(?<=\\\\\")[^\\\\;]+/";
$str = "\"{\"A5\";\"A6\";\"A7\";\"varying number of params...\"}\"";
preg_match_all($re, $str, $matches);
详细说明:
NODE EXPLANATION
--------------------------------------------------------------------------------
(?<= look behind to see if there is:
--------------------------------------------------------------------------------
\\ '\'
--------------------------------------------------------------------------------
" '"'
--------------------------------------------------------------------------------
) end of look-behind
--------------------------------------------------------------------------------
[^\\;]+ any character except: '\\', ';' (1 or more
times (matching the most amount possible))