Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP, Delete parts of a URL variable [duplicate]

Tags:

php

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

like image 399
RaduS Avatar asked Dec 07 '22 07:12

RaduS


1 Answers

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'];
like image 119
Lawrence Cherone Avatar answered Dec 22 '22 21:12

Lawrence Cherone