简体   繁体   中英

How to remove a specific series of characters from start of the url, if they exist

I'm using a snippet to pull the URL path using $_server[REQUEST_URI] .

Now I need to remove this block from it (lang_code) if it exists at the beginning: /en/

I'm fairly under trained in PHP for this. What function should I use?

If I use the trim function it keeps trimming until it hits a character that's not in the list, therefore butchering URLs that start with e or n.

I expect the snippet to pull the URL Path, and trim only the beginning and only if it starts with /en/ and only this block.

/en/new-test-url/ > new-test-url/

The code I have now is:

<?php echo preg_replace("/" . preg_quote("/en/",'/') . "/", , '$_SERVER['REQUEST_URI']'); ?>

Thanks in advance!

You can do something like this.

$myStr = '/en/new-test-url/';

// for singlebyte strings
$result = substr($myStr, 0, 4);
// for multibyte strings
$result = mb_substr($myStr, 0, 4);

if($result == '/en/'){ 
   $result = substr($myStr, 4);  
}else{
   $result = $myStr;
}
echo $result;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM