如何将preg_match_all用于此特定字符串?

时间:2015-04-13 20:41:41

标签: php regex

我可以使用preg_replace_all从

中提取“this is my string”
$this->translate("this is my string")

这是我到目前为止所做的:

preg_match_all("/_[_|e]\([\"|\']([^\"|\']+)[\"|\']\)/i", '$this->translate("this is my string")', array());

我想得到“这是我的字符串”如何做到这一点?

3 个答案:

答案 0 :(得分:0)

您可以使用以下正则表达式:

\(([^)]*)\)

Demo

答案 1 :(得分:0)

你可以不使用正则表达式,只需使用between函数(取自PHP manual):

<?php
    function after ($this, $inthat)
    {
        if (!is_bool(strpos($inthat, $this)))
        return substr($inthat, strpos($inthat,$this)+strlen($this));
    };
    function before ($this, $inthat)
    {
        return substr($inthat, 0, strpos($inthat, $this));
    };
    function between ($this, $that, $inthat)
    {
        return before ($that, after($this, $inthat));
    };

   echo between ("(\"", "\")", "\$this->translate(\"this is my string\")");
?>

这是sample program输出:

this is my string

答案 2 :(得分:0)

使用explode

$a = '$this->translate("this is my string")';
echo explode('"', $a)[1];

输出:

this is my string