我对PHP非常陌生,但我有这段代码:
$url = 'http://example.com/en-us/about-us';
if ($url contains 'en-us') {
replace 'en';
}
有谁知道我该如何让这个工作。
基本上,如果URL包含en-us
,则将URL替换为en
谢谢
$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
}
检查字符串是否包含特定单词的简单逻辑如下:
<?php
$word = "words";
$mystring = "this is collection of words";
// Test if string contains the word
if(strpos($mystring, $word) !== false){
echo "Word Found!";
} else{
echo "Word Not Found!";
}
?>
str_replace
http://php.net/str_replace - jszobodyexample.com/en-us/pen-usability
变成example.com/en/penability
... - Pevara