简体   繁体   中英

Echoing Only The Page Name (No Query)

I'm trying to check my php url up to the point before the query string begins.

Currently I have this: http://example.com/my-post-here.php?utm_source=webclick&utm_ad_id=123 so im only trying to check for my-post-here.php

The code I'm working with so far is:

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

This has worked fine up until i added the code after my-post-here.php so how do I still continue to check for only my-post-here.php and disregard everything else?

Sounds like you're looking for the basename of the url without it's query parameters.

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);

result:

my-post-here.php

You can also use parse_url as others have noted, but you'll need to strip the / character from what it returns.

Use the php explode function to separate the string at the question mark:

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

Here's a way to do it :

$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

You should read about the parse_url function here :

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM