4

I have the following php variable

$currentUrl

This php variable returns me the current url page. For example: it returns:

http://example.com/test-category/page.html?_ore=norn&___frore=norian

What php code can i use that will take this url link and delete everything after ".html" and would return me a clean url link, for example:

http://example.com/test-category/page.html

This would be returned in a new variable $clean_currentUrl

RaduS
  • 2,465
  • 9
  • 44
  • 65

3 Answers3

13

With PHP's parse_url()

<?php 
$url = "http://example.com/test-category/page.html?_ore=norn&___frore=norian";
$url = parse_url($url);

print_r($url);
/*
Array
(
    [scheme] => http
    [host] => example.com
    [path] => /test-category/page.html
    [query] => _ore=norn&___frore=norian
)
*/
?>

Then you can build your desired url from the values.

$clean_url = $url['scheme'].'://'.$url['host'].$url['path'];
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
  • Your sugestion also works very well. Thank you for your answer – RaduS Jun 23 '13 at 00:26
  • 6
    This should have been the accepted answer. It's the right tool for the job. – TecBrat Jun 23 '13 at 00:30
  • 2
    Hi TecBrat, It is true, this is also the right answer and i wish i can accept them both as right. I will use both parse_url() and preg_match for different purposes. The important think is that i learned about both of these solutions – RaduS Jun 23 '13 at 00:39
  • I am using "parse_url($currentUrl2, PHP_URL_PATH);" it returns me "example.com/test-category/page.html" how do i make it also put the "http://" so it would return me "http:// example.com/test-category/page.html" – RaduS Jun 23 '13 at 01:20
  • just to answer my own question from above, this is the solution: "$clean_url = parse_url($url, PHP_URL_SCHEME).'://'.parse_url($url, PHP_URL_HOST).parse_url($url, PHP_URL_PATH);" – RaduS Jun 23 '13 at 01:57
  • @RaduS Check my the last part of my answer, you dont use `parse_url()` multiple times, as parse url returns an array, then you use the array values to build you `$clean_url`. – Lawrence Cherone Jun 23 '13 at 13:24
1
$parts = explode('?', $currentUrl);
$url = $parts[0];
Expedito
  • 7,771
  • 5
  • 30
  • 43
1

Something like this:

<?php
$currentUrl = 'http://example.com/test-category/page.html?_ore=norn&___frore=norian';

preg_match('~http:\/\/.*\.html~', $currentUrl, $matches);
print_r($matches);

See amigura's comment below. To handle that case change the regex:

<?php
$currentUrl = 'http://example.com/test-category/page.html?_ore=norn&___frore=norian';

preg_match('~(http:\/\/.*\..+)\?~', $currentUrl, $matches);
print_r($matches);
Ruben
  • 5,043
  • 2
  • 25
  • 49
  • Thank you Ruben for the code, it does return a clean url but it also adds some other stuff on it. This is what it returns using your code:" Array ( [0] => example.com/test-category/page.html ) " – RaduS Jun 23 '13 at 00:19
  • How can i make it return a clean "http://example.com/test-category/page.html?" without the "Array ( [0] =>" in the begging and without the " ) " in the end – RaduS Jun 23 '13 at 00:20
  • The print_r was just to show you the result. You can actually grab the result by using $matches[0]: echo $matches[0]; – Ruben Jun 23 '13 at 00:21
  • The Array(... is the output of the print_r function, it's how it displays data. – Ruben Jun 23 '13 at 00:22
  • did you just change from the right answer to the wrong answer. with `parse_url()` the extention does not matter but with this it does. try changing html to php to see what i mean. – amigura Jun 23 '13 at 00:31
  • @amigura: please read the question "What php code can i use that will take this url link and delete everything after ".html"" – Ruben Jun 23 '13 at 00:32
  • @Ruben yes i know but i was saying if you wanted to do other url links this would fail. – amigura Jun 23 '13 at 00:35
  • @amigura okay fair enough, modified the answer to handle this. :) – Ruben Jun 23 '13 at 00:41
  • Guys, please don't argue over the right answer. I am thankful that i learned about both. – RaduS Jun 23 '13 at 00:41
  • @RaduS No problem. amigura had a good point actually. – Ruben Jun 23 '13 at 00:44
  • @RaduS i was not arguing just stating in my opinion a possible problem but its fix now all good. – amigura Jun 23 '13 at 00:49