Click here to Skip to main content
14,659,903 members
Rate this:
Please Sign up or sign in to vote.
See more:

$host = ltrim($_SERVER['REQUEST_URI'], '/');

this remove prefix “/” from URL

i also need the suffix “?page=*” removed

eg :

/city?page=4 becomes city

/sydney?page=444 becomes sydney


What I have tried:

tried preg-replace but could not combine with ltrim
Updated 2 days ago

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

You need to use urlencode & urldecode for it.

urlencode($your_string) — This PHP function is encodes a string to be used in a query part of a URL. URL encoding is used when placing text in a query string to avoid it being confused with the URL itself.

urldecode($your_string) — This PHP function is decodes the query part of a URL string.

$url = "https://sample.ex/home.php?queryparam=all about php";
$encodedUrl = urlencode($url);

//returns http%3A%2F%2Fsample.ex%2Fhome.php%3Fqueryparam%3Dall+about+php
echo $encodedUrl;

//returns https://sample.ex/home.php?queryparam=all about php
echo urldecode($encodedUrl);

Refer: PHP: urlencode - Manual[^]
PHP: urldecode - Manual[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100