【问题标题】:Echoing Only The Page Name (No Query)仅回显页面名称(无查询)
【发布时间】:2016-08-03 14:19:50
【问题描述】:

我正在尝试在查询字符串开始之前检查我的 php url。

目前我有这个:http://example.com/my-post-here.php?utm_source=webclick&utm_ad_id=123 所以我只是想检查my-post-here.php

到目前为止我正在使用的代码是:

$url = trim($_SERVER["REQUEST_URI"], '/');
echo $url;

这一直很好,直到我在my-post-here.php 之后添加了代码,那么我该如何继续只检查my-post-here.php 而忽略其他所有内容?

【问题讨论】:

标签: php


【解决方案1】:

这是一种方法:

$url = 'http://example.com/my-post-here.php?utm_source=webclick&utm_ad_id=123';
$parsed = parse_url($url); // parse the url
echo $parsed['path'];  // return /my-post-here.php

您应该阅读有关 parse_url 函数 here 的信息:

【讨论】:

    【解决方案2】:

    听起来您正在寻找不带查询参数的网址的basename

    http://php.net/manual/en/function.basename.php

    // your original url
    $url = 'http://example.com/my-post-here.php?utm_source=webclick&utm_ad_id=123';
    
    // we don't need the query params
    list($url, $queryParams) = explode("?", $url);
    
    // echo the basename
    echo basename($url);
    

    结果:

    my-post-here.php
    

    您也可以使用parse_url,正如其他人所指出的那样,但您需要从它返回的内容中去除/ 字符。

    【讨论】:

    • 第一次在php中看到函数''list()''! :o
    【解决方案3】:

    使用php的explode函数将问号处的字符串分开:

    $array = explode('?', $url);
    $newUrl = $array[0];
    echo $newUrl; //this will have your url before the question mark
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-12
      • 1970-01-01
      • 1970-01-01
      • 2019-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多