0

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

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 company@example.com but it gives me {{email_company}} instead...

Is it something not right in this regex?

"/\{{2}(([a-z]+\|.+)|([a-z\_]))\}{2}/Ue"
Run
  • 54,938
  • 169
  • 450
  • 748
  • I am sorry but what are your trying to do exactly. I don't know PHP but i might be able to help with the regex if I know what are you trying to achieve exactly ? – Ibrahim Najjar Aug 19 '13 at 08:40
  • thanks. 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? – Run Aug 19 '13 at 08:45
  • the current regex only grabs `youtube|id=ykwqXuMPsoc&width=560&height=315` but not `email_company`... – Run Aug 19 '13 at 08:46
  • Is that a small `u` or an uppercase `U` in the modifiers list ? – Ibrahim Najjar Aug 19 '13 at 08:49
  • it is an upper-case from the original code. – Run Aug 19 '13 at 08:50
  • So why aren't you just using `\{{2}(.*)\}{2}`. It will definitely catch anything between double curly braces ? – Ibrahim Najjar Aug 19 '13 at 08:55
  • I tested it. but i think it is to do with php as well as it won't work... :-( – Run Aug 19 '13 at 08:59

1 Answers1

2

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

Ibrahim Najjar
  • 19,178
  • 4
  • 69
  • 95