XHP with Regex for link Replacement

时间:2015-09-01 22:20:47

标签: php regex hacklang xhp

I am trying to implement a simple function that given a text input, returns the text modified with xhp_a when a link is detected, within a paragraph xhp_p.

Consider this class

class Urlifier {

    protected static $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";


    public static function convertParagraphWithLink(?string $input):xhp_p{
        if (!$input)
            return <p></p>;
        else
        {
            if (preg_match(self::$reg_exUrl,$input,$url_match)) //match found
            {
                return <p>{preg_replace($reg_exUrl, '<a href="'.$url_match[0].'>'.$url_match[0].'</a>', $input)}<p>;
            }else{//no link inside
                <p>{$input}</p>
            }
        }
}

The problem here is that xhp escapes html and links are not shown as expected. I suppose that this happens because a do not create a dom hierarchy as expected (with appendChild method for example) and thus everything regex replaces is a string.

So my other approach to this problem was to use preg_match_callback with a callback function that would create xhp_a and add to hierarchy under xhp_p but that did not work either.

Am i wrong somewhere ? If not would there by any security risk / bigger overhead by just finding and replacing on load the html on client side instead of server ?

Thanks for your time !

0 个答案:

没有答案