How to remove parameters from url in php

王林
Release: 2023-03-02 19:20:02
Original
5346 people have browsed it

php去掉url中的参数的方法是:可以通过trim()函数来实现。该函数可以删除字符串中的指定字符,并返回已修改的字符串。具体使用方法如:【trim($url,"?");trim($url,"#");】。

How to remove parameters from url in php

相关函数介绍:

(推荐教程:php教程

trim() 函数移除字符串两侧的空白字符或其他预定义字符,并返回已修改的字符串。

语法:

trim(string,charlist)
Copy after login

参数说明:

  • 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;  
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!