Search code examples
phpurlhttp-redirectstrtoklang

How to remove second slash URL and change parameter


please help me to fix this.

I have result url like this

mypath/index/id

And I want to remove "/id" and change to "/en"

I created this for removing

$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$dis_url=trim(strtok($url, '/'));

And I created link like this

if (isset($_SESSION['lang']) && $_SESSION['lang'] == 'en') {}
    echo " <a href='$dis_url-en'></a>";
if (isset($_SESSION['lang']) && $_SESSION['lang'] == 'id') {}
    echo "<a href='$dis_url-id'></a>";

Please help me to fix this.


Solution

  • You can use str_replace():

    $a='mypath/index/id';
    echo str_replace("/id", "/en",$a); //result:mypath/index/en