Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-过滤来自用户的URL';s消息仅包含来自父网站的链接_Php_Regex_Url_Yii - Fatal编程技术网

PHP-过滤来自用户的URL';s消息仅包含来自父网站的链接

PHP-过滤来自用户的URL';s消息仅包含来自父网站的链接,php,regex,url,yii,Php,Regex,Url,Yii,我正在研究一种消息过滤算法,允许来自父网站的链接,甚至以任何形式替换来自外部网站的最细微的链接 从PHP的角度来看,如果能提供一个健康的解决方案,我们将不胜感激 据我所知,这可以通过4个基本步骤来完成- 1.使用preg\u match\u all检测字符串中的URL 2.将它们清理为有效的URL格式 3.使用parse_url检查主机值并适当替换旧字符串。 4.将URL转换为html友好的可点击链接。 此外,如果Yii框架有帮助的话,我正在使用它 编辑:在这里粘贴我的代码,欢迎提出建议

我正在研究一种消息过滤算法,允许来自父网站的链接,甚至以任何形式替换来自外部网站的最细微的链接

从PHP的角度来看,如果能提供一个健康的解决方案,我们将不胜感激

据我所知,这可以通过4个基本步骤来完成-
1.使用preg\u match\u all检测字符串中的URL
2.将它们清理为有效的URL格式
3.使用parse_url检查主机值并适当替换旧字符串。
4.将URL转换为html友好的可点击链接。
此外,如果Yii框架有帮助的话,我正在使用它

编辑:在这里粘贴我的代码,欢迎提出建议

       $pattern = '(?xi)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`\!()\[\]{};:\'".,<>?«»“”‘’]))';
       $message = preg_replace_callback("#$pattern#i", function($matches) {
       $hostname = Yii::app()->params['baseUrlName'];
       $input = $matches[0];
       $url = preg_match('!^https?://!i', $input) ? $input : "http://$input";
       $data = parse_url($url);
       if(isset($data['host']) && $data['host']==$hostname)
        { return '<a href="' . $url . '" rel="nofollow" target="_blank">' .     "$input</a>"; } 
       else
        { return ' x '; }    }, $message);
$pattern='(?xi)\b((?:https?://)[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()]+\([^\s()]+\([^\s()])*)+(?:([^\s()]+\)+(([^\s())+)+([^\s())+)*([^\s())+)*([^\s())+)+)*([^-+)+)+(^-+)+)*([^-+)+)*(^-[^+)+)+)+)+(^-[^-[^-+)+)+)+)+;
$message=preg_replace_回调(“#$pattern#i”,函数($matches){
$hostname=Yii::app()->params['baseUrlName'];
$input=$MATCHS[0];
$url=preg_match(“!^https?:/!i”,$input)?$input:“http://$input”;
$data=parse_url($url);
如果(isset($data['host'])&&&$data['host']==$hostname)
{return'”;}
其他的
{返回'x';}},$message);

这是
$pattern
的图像:

(?xi)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`\!()\[\]{};:\'".,<>?«»“”‘’]))
(?xi)/b((?:https?:/)(?:[^\s()]+)*)+(?:([^\s())+)+(([^\s())+)*)+(([^\s())+)+([^\s())+([^\s())+)+((?:(([^\s()+)+)+)*)+((([^\s())+)+)+((^\s())+)+)+(^-+)+(^-+)+)+(^-+)+)+(^-+)+)+(^+)+)+)*(^-(^-)+)+)+(^-(^-)+)+)+)+(^-(^-(^-)+)+)+)+)+(^-(^-)+)

它可以这样减少:

(?xi)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:(?P<BPC>[^\s()<>]+)|\(((?&BPC)|(\((?&BPC)\)))*\))+(?:\(((?&BPC)|(\((?&BPC)\)))*\)|[^\s`\!()\[\]{};:\'".,<>?«»“”‘’]))
(?xi)/b((?:https?:/)/(www\d{0,3}[。])[a-z0-9.-]+[。]a-z]{2,4}/(?:(?P[^\s())+)\((?&BPC);((((?&BPC)\)*)+((?:(((((?&BPC);((((?&BPC)\)))(((((((?&BPC)\))))*):(((((((((?&BPC);)))):)*):((((((((?&BPC)())))))):):):)

这是目前最好的解决方案

       $pattern = '(?xi)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:(?P<BPC>[^\s()<>]+)|\(((?&BPC)|(\((?&BPC)\)))*\))+(?:\(((?&BPC)|(\((?&BPC)\)))*\)|[^\s`\!()\[\]{};:\'".,<>?«»“”‘’]))';
       $message = preg_replace_callback("#$pattern#i", function($matches) {
       $hostname = Yii::app()->params['baseUrlName'];
       $input = $matches[0];
       $url = preg_match('!^https?://!i', $input) ? $input : "http://$input";
       $data = parse_url($url);
       if(isset($data['host']) && $data['host']==$hostname)
        { return '<a href="' . $url . '" rel="nofollow" target="_blank">' .     "$input</a>"; } 
       else
        { return ' x '; }    }, $message);
$pattern='(?xi)b((?:https:/)/(www.d{0,3}[.].[a-z0-9.\-]+[.][a-z]{2,4}/)(?:(?P[^\s()]+)(((?&BPC);(\(((?&BPC)\))*)+(?:\(((((?&BPC)\)(((?&BPC)\)-)(((?&BPC)\)*)*)(?:((?&BPC)\)(((?&BPC)\)))*)(?)*)(?)!!!);
$message=preg_replace_回调(“#$pattern#i”,函数($matches){
$hostname=Yii::app()->params['baseUrlName'];
$input=$MATCHS[0];
$url=preg_match(“!^https?:/!i”,$input)?$input:“http://$input”;
$data=parse_url($url);
如果(isset($data['host'])&&&$data['host']==$hostname)
{return'”;}
其他的
{返回'x';}},$message);

更新-使用Alex的正则表达式调整进行编辑。

这些基本步骤听起来相当不错。你的问题是什么,你在坚持什么?不是坚持目前的方法本身,只是担心这在长消息中会变得太麻烦,并希望通过PHP提供一个替代的更短、更高效的解决方案。我不认为有一个简单的PHP功能可以实现这一点。如果您想捕获相对URL(在您的规则下是可以的)、协议相对URL(您将检查它们指向何处…)之类的情况,那么它可能会变得更加复杂。是的,第一步的正则表达式非常长。知道任何替代解决方案都会非常棒:)用现在的解决方案更新了我的问题。减少是有意义的,谢谢。