正则表达式匹配url php,PHP 字符串中匹配url

Java 大概这么写

String str = "接收到的字符串"

String regex = "(http:|https:)//[^[A-Za-z0-9,:\\._\\?%&+\\-=/#]]*";

Pattern pattern = Pattern.compile(regex);

Matcher matcher = pattern.matcher(str);

while (matcher.find()) {

String url=matcher.group();

System.out.println(url);

}

以下字符串通过测试.

String str="http://127.0.0.1:6666/ " +

"https://www.baidu.com/ " +

"http://127.0.0.1/metinfo/img/img.php?class1=1&serch_sql=%201=if%28ascii%28substr%28user%28%29,1,1%29%29=114,1,2%29%23\n" +

"或者\n" +

"哈哈http://www.baidu.com:85676/metinfo/img/img.php?class1=1&serch_sql=%201=if%28ascii%28substr%28user%28%29,1,1%29%29=114,1,2%29%23 6666都是对的";

输出

http://127.0.0.1:6666/

https://www.baidu.com/

http://127.0.0.1/metinfo/img/img.php?class1=1&serch_sql=%201=if%28ascii%28substr%28user%28%29,1,1%29%29=114,1,2%29%23

http://www.baidu.com:85676/metinfo/img/img.php?class1=1&serch_sql=%201=if%28ascii%28substr%28user%28%29,1,1%29%29=114,1,2%29%23

什么,你问的式PHP?抱歉,我不会PHP。。。

正则一样的,自己动动脑袋吧。

已标记关键词 清除标记
表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页