首页 > PHP教程 > php开发知识文章

PHP替换字符为其他字符的实现方法

正则表达式

$str = "/php/";  // 关键字正则字符串  

$string = "hello php";    // 文本字符串 

echo preg_replace($str, "c", $string);    //preg_replace() 执行一个正则表达式的匹配和替换

 

strpos和strtr函数

strpos($string,"php");  //  查询是否存在字符串

$arr = array("php"=>"c");

echo strtr($string,$arr);

关闭
感谢您的支持,我会继续努力!
扫码打赏,建议金额1-10元


提醒:打赏金额将直接进入对方账号,无法退款,请您谨慎操作。