PHP regex - single quote doesn't work - TWIG pre-escaping

I am having a problem with single quotes in regex. What I want to do is replace the emoticons in the line with an html image tag. All emoticons work except sad emoticon: '- (because it has one quote. Magic quotes are disabled (testet with if (g! Et_magic_quotes_gpc ()) dd (' mq off ');).

So let me show you some code.

    protected $emoticons = array(
        // ...
        'cry' => array(
            'image' => '<img class="smiley" src="/image/emoticon/cry.gif" />',
            'emoticons' => array(":'(", ";'(", ":'-(", ";'-(")
        ),
    );

      

My method of replacing all emoticons is as follows:

    public function replaceEmoticons($input) {

        $output = $input;

        foreach ($this->emoticons as $emo_group_name => $emo_group) {

            $regex_emo_part = array();

            foreach ($emo_group['emoticons'] as $emoticon) {
                $regex_emo_part[] = preg_quote($emoticon, '#');
            }

            $regex_emo_part = implode('|', $regex_emo_part);
            $regex = '#(?!<\w)(' . $regex_emo_part .')(?!\w)#';
            $output = preg_replace($regex, $emo_group['image'], $output);

        }

        return $output;

    }

      

But as I said: "kills". There is no substitute. :-): - / etc. work. What for? FYI Content of $ regex:#(?!<\w)(\:\'\(|;\'\(|\:\'\-\(|;\'\-\()(?!\w)#

What's wrong here, can you help me?

UPDATE:

Thanks @cheeryi and cychoi. The replacement method is ok, you did it. I found the problem. My string is flushed before it gets redirected to the replaceEmoticons method. I am using TWIG templating engine and I am using nl2br filter in front of my homemade replace_emoticon filter. Let me show you. This is the result in the final template. This is the template for showing the comment for a blog post:

{{ comment.content|nl2br|replace_emoticons|raw }}

      

Problem: nl2br automatically pre-escapes the input string, so "is replaced with an escaped character" # 039;

I need nl2br to display strings as <br /> - and I need escaping too to disable html tags in user input. I need replace_emoticons to replace my emoticons (TWIG plug-in extension). And I also need the raw html here at the end of the filter chain, otherwise all the HTML emoticons img tags will be escaped and I will see the raw html in the comment text.

What can I do here? The only problem here seems to be that nl2br is escaping. "It's not a bad idea, but in my case, it will destroy any sad emoji that contains" in it. "

Looking for a solution to solve the problem and I hope you can help me.

Best, titanium

+3


source to share


1 answer


I added an optional parameter to the smiley method:

public function replaceEmoticons($input, $use_emo_encoding_for_regex = true) {

      

and I changed the foreach part to lil 'bit:



foreach ($emo_group['emoticons'] as $emoticon) {

    if ($use_emo_encoding_for_regex === true) {
        $emoticon = htmlspecialchars($emoticon, ENT_QUOTES);
    }

    $regex_emo_part[] = preg_quote($emoticon, '#');

}

      

It works! All emoticons have been replaced!

0


source







All Articles