Run
Run

Reputation: 57196

php: writing a curly parser

I found this idea of curly parser online and I want to further develop it. The original parser is made for passing 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 '[email protected]';
                else if($input === 'email_personal') return '[email protected]';
        }

        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>";
    }
}

example usage that works,

$curly = new curly();
$input = '<p>{{youtube|id=ykwqXuMPsoc&width=560&height=315}}</p>'; 
echo $curly->replace($input); 
// return <p><iframe width="560" height="315" src="http://www.youtube.com/embed/ykwqXuMPsoc" frameborder="0" allowfullscreen></iframe></p>

another example that does not work,

$input = '<p>{{email_company}}</p>'; 
echo $curly->replace($input);
// return <p>{{email_company}}</p>

when it is {{email_company}} then it should return [email protected] but it gives me {{email_company}} instead...

Is it something not right in this regex?

"/\{{2}(([a-z]+\|.+)|([a-z\_]))\}{2}/Ue"

Upvotes: 0

Views: 66

Answers (1)

Ibrahim Najjar
Ibrahim Najjar

Reputation: 19423

I think I need a regex that is able to grab email_company or youtube|id=ykwqXuMPsoc&width=560&height=315 from the double curly brackets. is it possible?

Yes it is possible. You can simply use this regex: \{{2}([^}]*)\}{2} and you can check it at this Regex 101 Demo.

If for some reason you don't like this expression or as you said in your comment it did't work for you (which confuses me why ?) then you can modify the original expression you have posted to the following one and everything shell work fine:

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

And here is the Regex 101 Demo

Upvotes: 2

Related Questions