Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php:编写卷曲解析器_Php_Regex_Preg Replace_Php 5.4 - Fatal编程技术网

php:编写卷曲解析器

php:编写卷曲解析器,php,regex,preg-replace,php-5.4,Php,Regex,Preg Replace,Php 5.4,我是在网上发现这个卷曲解析器的想法的,我想进一步开发它。原始解析器用于传递youtube class curly { /** * Replace the macros in an input string * @param string $input * @return string */ public function replace ($input) { //return preg_replace("/\{{2}([a

我是在网上发现这个卷曲解析器的想法的,我想进一步开发它。原始解析器用于传递youtube

class curly 
{
    /**
     * Replace the macros in an input string
     * @param string $input
     * @return string
     */
    public function replace ($input) {
        //return preg_replace("/\{{2}([a-z]+\|.+)\}{2}/Ue",'$this->_replace("\\1")',$input); // original
        return preg_replace("/\{{2}(([a-z]+\|.+)|([a-z\_]))\}{2}/Ue",'$this->_replace("\\1")',$input);
    }

    /**
     * Run the replacement code on a given macro string
     * @param string $input
     * @return string
     */
    private function _replace ($input) {

        print_r($input);

        list ($name,$params) = explode("|",$input);

        if (method_exists($this,$name)) {
            return $this->$name($params);
        }else{
            if($input === 'email_compnay') return 'company@example.com';
                else if($input === 'email_personal') return 'personla@example.com';
        }

        throw new Exception ("Unrecognised macro: {$name}.",500);
    }

    /**
     * Replaces a YouTube curly
     * @param string $params
     * @return string
     */
    private function youtube ($params) {
        parse_str($params);

        // set defaults
        if (!isset($id)) { $id = "ykwqXuMPsoc"; }
        if (!isset($width)) { $width = 560; }
        if (!isset($height)) { $height = 315; }

        // output the final HTML
        return "<iframe width=\"{$width}\" height=\"{$height}\" src=\"http://www.youtube.com/embed/{$id}\" frameborder=\"0\" allowfullscreen></iframe>";
    }
}
我想我需要一个正则表达式,能够从双花括号中抓取email_company或youtube | id=ykwqXuMPsoc&width=560&height=315。可能吗

是的,这是可能的。您可以简单地使用这个正则表达式:
\{{2}([^}]*)\}{2}
,您可以在这里检查它

如果出于某种原因,您不喜欢此表达式,或者正如您在评论中所说,它对您不起作用(这让我很困惑,为什么?),那么您可以将您发布的原始表达式修改为以下表达式,并且所有内容都可以正常工作:

/\{{2}(([a-z]+\|.+)|([a-z\_]+))\}{2}/Ue
//                          ^
//                          |
//                    Notice the added + sign, your previous expression only
//                    matched a single character.

这是

对不起,你到底想做什么。我不懂PHP,但如果我知道你到底想实现什么,我可能可以帮助你使用正则表达式。谢谢。我想我需要一个正则表达式,它能够从双花括号中抓取
email_company
youtube | id=ykwqXuMPsoc&width=560&height=315
。有可能吗?当前的正则表达式只抓取youtube上的
id=ykwqXuMPsoc&width=560&height=315
,而不抓取
email\u company
…这是修饰符列表中的一个小的
u
还是大写的
u
?它是原始代码的大写字母。谢谢。添加一个
+
,它就可以完美地工作了!非常感谢!有时我会在字符串中再次出现花括号-“{p>{email_company}{email_personal}}

`和
{youtube | id=ykwqXuMPsoc&width=560&height=315}

{youtube | id=ykwqXuMPsoc&width=560&height=315}

{?
$input = '<p>{{email_company}}</p>'; 
echo $curly->replace($input);
// return <p>{{email_company}}</p>
"/\{{2}(([a-z]+\|.+)|([a-z\_]))\}{2}/Ue"
/\{{2}(([a-z]+\|.+)|([a-z\_]+))\}{2}/Ue
//                          ^
//                          |
//                    Notice the added + sign, your previous expression only
//                    matched a single character.