如何使PHP JSON_DECODE在单引号中接受参数



我有一个我可以从可供用户提供的特定短码获得的参数列表。

用户输入的示例:这是我的名称列表:{{{名称(" John"," Mike"," O'Connor")}}}

从中,我有一个以下等级来提取参数列表,因此我得到了一个类似的字符串:"约翰",'Mike'," O'Connor"

,但是现在我需要将此列表转换为一个数组,我可以认为的最好的方法是使用JSON_DECODE,因为某些用户只需使用单个引号,就可以破坏。

将此字符串转换为数组的最佳方法是什么?使用str_replace替换为"也将打破" o'Connor"参数。

如何用下面的正则提取名称?

$re = '/(["'])((?:(?!1).)*)1/';
$str = '{{names("John", 'Mike', "O'connor")}}';
preg_match_all($re, $str, $matches, PREG_PATTERN_ORDER, 0);
// Print the entire match result
var_dump($matches);

输出:

array (size=3)
  0 => 
    array (size=3)
      0 => string '"John"' (length=6)
      1 => string ''Mike'' (length=6)
      2 => string '"O'connor"' (length=10)
  1 => 
    array (size=3)
      0 => string '"' (length=1)
      1 => string ''' (length=1)
      2 => string '"' (length=1)
  2 => 
    array (size=3)
      0 => string 'John' (length=4)
      1 => string 'Mike' (length=4)
      2 => string 'O'connor' (length=8)

名称可在$matches[0]上获得,带引号或$matches[2]无引号。

最新更新