用于使用参数解析方法的正则表达式

时间:2015-07-03 14:21:37

标签: php regex expression

我有这样的内容,例如:

<?php echo $this->translate("Any text", "Any text next", "base-key") ?> Next string bla bla 
string bla <?php echo $this->translate("SO Any text") ?> 
enter code heretext bla <?php echo $this->translate("and string", null, "article-key") ?>

所以,我需要一个正则表达式,它匹配方法“translate”的第一个和第三个参数中的引号之间的文本。

输出可能如下所示:

array (
  array (
    0 => Any text,
    1 => base-key
  ),
  array (
    0 => SO Any text,
    1 => null
  ),
  array (
    0 => and string,
    1 => article-key
  ),
)

我正在尝试这个表达式:

/translate\(["\'](.*?)["\']/s

我只是得到第一个参数字符串方法“translate”

1 个答案:

答案 0 :(得分:3)

取得了一些成功:translate\(\"?(.*?)\"?(?:,\s?(?:\"?.*?\"?))?(?:,\s?\"?(.*?)\"?)?\)。我编写它的方式,它也会捕获第一个和第三个参数,如果它们不在引号中,你可以通过删除第一个和第三个参数引号中的?并使第一个参数可选来撤消它 - {{1} }

可在此处测试:https://regex101.com/r/kG9eF3/1

相关问题