1

当我尝试使用正则表达式使用模式动态创建链接时,我得到了无效的 html。

这是我创建链接的函数:

$message ="sddsdsd @@[Sanesh Sunny:ZGNjBQN9ac3K] dsdsdsd ";
    function convert_usertags_links($message,$extraParams){
            $tag_name = 'a'; //default a 
            $has_link = $new_tab = 0; //default no link
            $href = $target = $sel_id = $tag_col = "";
            $tag_col = 'tag_col';
            if(isset($extraParams['tag_name']) && $extraParams['tag_name']!="")
                $tag_name = $extraParams['tag_name'];
            if(isset($extraParams['tag_col']) && $extraParams['tag_col']!="")
                $tag_col = $extraParams['tag_col'];
            if(isset($extraParams['has_link']) && $extraParams['has_link']!="")
                $has_link = $extraParams['has_link'];
            if(isset($extraParams['new_tab']) && $extraParams['new_tab']!="")
                $new_tab = $extraParams['new_tab'];
            if(isset($extraParams['selector_id']) && $extraParams['selector_id']!="")
                $sel_id = $extraParams['selector_id'];
            $site_url = 'www.google.com/project/';
            if($has_link == 1)
                $href = " href='".$site_url."user/profile/$2' ";
            if($has_link == 1)
                $target = " target='_blank' ";
            /*
             * to match numbers only       
             */
            $pattern = "/@@\[([^:]*):(.*?)\]/"; 
            #$pattern = "/@@\[([^:]*):([0-9a-zA-Z]*)\]/";
            $matches = array();
            preg_match_all($pattern, $message, $matches);
            $output = preg_replace($pattern, "<".$tag_name." class='".$tag_col."' id='".$sel_id."_$2'".$href.$target." >$1</".$tag_name.">", $message);
            return $output;
        }

此功能在 localhost 中完美运行,但是当我在具有类似“ https://dsdsdsdsdsdsd.com/project ”的site_url 的临时服务器中尝试相同时,我得到了无效的html,例如:

<a class="tag_col" id="user_tag_link_ZGV3ZmZ9ac3K_ZGNjBQN9ac3K" href="&lt;a href=&quot;www.google.com/4thambit-v6/user/profile/ZGNjBQN9ac3K&quot; target=&quot;_blank&quot;&gt;www.google.com/4thambit-v6/user/profile/ZGNjBQN9ac3K&lt;/a&gt;" target="_blank">Sanesh Sunny</a>

href包含另一个a标签...如果我将site_url作为IP地址传递给192.168.1.X,它工作正常但是当我在sit_url中使用“www”或“.com”时,上面的无效html与另一个返回href里面的一个标签......

知道为什么会这样吗?我尝试了几个网站,但没有帮助....

4

0 回答 0