2016-02-29 78 views
0

很新的PHP,但我有這樣的代碼PHP變量包含字符串和替換

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

    if ($url contains 'en-us') { 
     replace 'en'; 
    } 

沒有人知道我會得到這個工作。

基本上,如果URL包含en-usen

感謝

+0

看看'str_replace' HTTP更換網址:// php.net/str_replace – jszobody

+1

[strpos()](http://www.php.net/manual/en/function.strpos.php)和[str_replace()](http://www.php.net/manual /en/function.str-replace.php)... [PHP文檔](http://www.php.net/manual/en/index.php)令人難以置信的是我們充滿了詳細的解釋和如何使用每個功能的例子...學會使用它們......你應該真正使用[PHP文檔](http://www.php.net/manual/en/index.php )請求其他人花時間回答你的問題 –

+0

http://php.net/manual/en/ref.strings.php –

回答

2
$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 
} 

我希望這可能是一些幫助你