2016-01-20 51 views
-1
$replace = 'replace'; 
$my_string = ' abC.com, Abc.net, aBc.org, ABC.info, aBC.biz '; 
$lowerCase = 'abc'; 
$str_replace = str_replace($lowerCase, $replace, $my_string); 
echo $str_replace; 

我需要的结果如下:rep​​lace.com,replace.net,replace.org,replace.info,replace.bizPHP str_replace函数不能按预期工作

+2

使用'str_ireplace'。 http://php.net/manual/en/function.str-ireplace.php – smoqadam

回答

0

由于PHP manual says

str_ireplace - 不区分大小写的str_replace()版本。

所以,你应该用str_ireplace

$str_replace = str_ireplace($lowerCase, $replace, $my_string); 

但要注意,该字符案件正在由服务器的区域设置,它会影响含非ASCII字符的字符串定义。

您还可以使用strtolower

$str_replace = str_replace($lowerCase, $replace, strtolower($my_string)); 
0

strtolower() $ my_string和编写代码的结果。 str_replace - 替换为替换字符串搜索字符串的所有出现(PHP 4,PHP 5,PHP 7)

$replace = 'replace'; 
$my_string = ' abC.com, Abc.net, aBc.org, ABC.info, aBC.biz '; 
$lowerCase = 'abc'; 
$str_replace = str_replace($lowerCase, $replace, strtolower($my_string)); 
echo $str_replace;