0

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?

Joe Bobby
  • 2,803
  • 10
  • 40
  • 59
  • Try `$_SERVER['SCRIPT_FILENAME']` maybe ? http://php.net/manual/en/reserved.variables.server.php. Additionally, you can always try `var_dump($_SERVER)` – Maximus2012 Apr 12 '16 at 19:26
  • 1
    http://php.net/manual/en/function.parse-url.php – Axalix Apr 12 '16 at 19:26
  • 2
    Possible duplicate of [Request string without GET arguments in PHP](http://stackoverflow.com/questions/9504608/request-string-without-get-arguments-in-php) – devlin carnate Apr 12 '16 at 19:26

3 Answers3

1

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.

skrilled
  • 5,350
  • 2
  • 26
  • 48
0

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
Nick Suwyn
  • 501
  • 1
  • 5
  • 21
0

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 :

SamyQc
  • 365
  • 2
  • 10