Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
QUESTION :

$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

etc


What I have tried:

tried preg-replace but could not combine with ltrim
Posted
Updated 17-Oct-20 5:40am

1 solution

You need to use urlencode & urldecode for it.

Quote:
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.

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


Example:
PHP
$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[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900