Home >Backend Development >PHP Problem >How to remove parameters from url in php
php去掉url中的参数的方法是:可以通过trim()函数来实现。该函数可以删除字符串中的指定字符,并返回已修改的字符串。具体使用方法如:【trim($url,"?");trim($url,"#");】。
相关函数介绍:
(推荐教程:php教程)
trim() 函数移除字符串两侧的空白字符或其他预定义字符,并返回已修改的字符串。
语法:
trim(string,charlist)
参数说明:
string 必需。规定要检查的字符串。
charlist 可选。规定从字符串中删除哪些字符。如果省略该参数,则移除下列所有字符:
代码实现:
function removeqsvar($url, $var_names) { foreach($var_names as $param){ $url = preg_replace('/([?&])'.$param.'=[^&]+(&|$)/','$1',$url); } $url = trim($url,"?"); $url = trim($url,"#"); $url = trim($url,"&"); $url = trim($url,"/"); return $url; }
The above is the detailed content of How to remove parameters from url in php. For more information, please follow other related articles on the PHP Chinese website!