php变量包含字符串并替换

Bar*_*gan 3 php contains

对 PHP 很陌生,但我有这段代码

$url =  'http://example.com/en-us/about-us';

    if ($url contains 'en-us') {
        replace 'en';
    }
Run Code Online (Sandbox Code Playgroud)

有谁知道我如何让它发挥作用。

基本上,如果 URL 包含,en-us请将 url 替换为en

谢谢

Vas*_*dix 6

$url =  'http://example.com/en-us/about-us';

if (strpos($url,'en-us') !== false) { //first we check if the url contains the string 'en-us'
    $url = str_replace('en-us', 'en', $url); //if yes, we simply replace it with en
}
Run Code Online (Sandbox Code Playgroud)

我希望这对你有帮助