首页 > 解决方案 > 使用 Preg_Match 提取特殊字符之间的两个值

问题描述

您好,我想用一个 preg_match_all 提取用户名和密码值

$url='http://xxxxxxxx.com:80/get.php?username=xxxxxx&password=xxxxxx&type=m3u_plus';

我得到了我想要的爆炸,但我知道 preg_match_all 效果更好,你能告诉我怎么做吗?

$url_ext = parse_url($url);
$username=explode ("&",explode("=",$url_ext['query'])[1]);
$password=explode ("&",explode("=",$url_ext['query'])[2]);

我尝试使用此代码但无法正常工作

$lotes_fil='~https?://.\=(.+?)&~';
preg_match_all($lotes_fil,$url,$link_pre);

标签: special-characterspreg-match-all

解决方案


最好使用parse_urlparse_str就像这里解释的那样。

但是,如果你真的想要一个正则表达式,这可以完成工作:

(?<=[?&])([^&=\r\n]+)=([^&=\r\n]+)

preg_match_all('/(?<=[?&])([^&=\r\n]+)=([^&=\r\n]+)/', $url, $matches);

参数名称在第 1 组,值在第 2 组

演示和解释


推荐阅读