200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > 正则表达式 – PHP 字符串中匹配url

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

时间:2021-06-13 04:11:20

相关推荐

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

后端开发|php教程

php,url,正则表达式

后端开发-php教程

需求:

hello html5 源码,vscode背景小麦色,ubuntu 网口,tomcat被谁启动,发现爬虫,lnmp php 扩展,江阴seo优化哪家好,二手房中介网站源码下载,shop模板网站lzw

客户端传过来一段字符串,需要从字符串中匹配出所有的url,包括域名或IP后面的参数(含端口)

URL样例:

飘零1.5源码,ubuntu18 acpi,基础爬虫的作用,php post[,岑溪seo推广lzw

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或者/metinfo/img/img.php?class1=1&serch_sql=%201=if%28ascii%28substr%28user%28%29,1,1%29%29=114,1,2%29%23

当然简单URL也是要匹配出来的

求解正则

c tcp客户端 源码,ubuntu设置开机执行,tomcat中amd64,python爬虫 抢购,php json数据在哪里,SEO导师lzw

回复内容:

需求:

客户端传过来一段字符串,需要从字符串中匹配出所有的url,包括域名或IP后面的参数(含端口)

URL样例:

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或者/metinfo/img/img.php?class1=1&serch_sql=%201=if%28ascii%28substr%28user%28%29,1,1%29%29=114,1,2%29%23

当然简单URL也是要匹配出来的

求解正则

先用比较宽泛的正则匹配出所有的url,例如

https?:\/\/\S+

然后对于这堆url依次采用parse_url函数

^(http|https|ftp)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\/\\\+&%\$#\=~])*$/Search.aspx?k=url&c=-1&m=5&ps=20

Java 大概这么写

String str = "接收到的字符串"String regex = "(http:|https:)//[^[A-Za-z0-9,:\\._\\?%&+\\-=/#]]*"; Pattern pattern = pile(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/ " +"/ " +"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" +"哈哈: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//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: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。。。

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。