PHP Regex preg_replace

gee*_*ide 0 php preg-replace

我想替换这个URI

http://localhost/prixou/index.php?page=list&category=1&sub=1&subsub=0&brand=Sony&toto=titi
Run Code Online (Sandbox Code Playgroud)

通过这个URI

http://localhost/prixou/index.php?page=list&category=1&sub=1&subsub=0&kljklj=sdfsd
Run Code Online (Sandbox Code Playgroud)

==>我想删除"&brand = Sony"

我试过这个:

preg_replace('/(^.*)(&brand=.*)(&.*)/', '$1$3', 'http://localhost/prixou/index.php?page=list&category=1&sub=1&subsub=0&brand=Sony&toto=titi');
Run Code Online (Sandbox Code Playgroud)

但它在特定情况下不起作用:URI中的参数"toto"不存在的情况

所以,如果我这样做

preg_replace('/(^.*)(&brand=.*)(&.*)/', '$1$3', 'http://localhost/prixou/index.php?page=list&category=1&sub=1&subsub=0&brand=Sony'); 
Run Code Online (Sandbox Code Playgroud)

它不起作用==>"&brand = Sony"仍然出现

那我该怎么办?

Tre*_*ama 6

我不会使用正则表达式.

首先,使用parse_url将url拆分为多个部分.
然后,在查询部分使用parse_str.

对查询键执行任何操作,然后将其全部组合.
要重新构建查询字符串:http_build_query
然后使用http_build_url构建URL