您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php如何进行字符串匹配替换

2025/12/3 10:38:17发布18次查看
php字符串匹配替换的方法:执行一个正则表达式搜索并且使用一个回调进行替换,代码为【preg_replace_callback()return  preg_replace_callback('/\\{([\w\-\/]+)\\}/'】。
php字符串匹配替换的方法:
<?php $templ = "{username}是个大{adj}比{end}";//测试字符串 //对应数据 $datas = [ 'username' => '我', 'adj' => '帅', 'end' => '。', ]; //不需要替换的字符串 $nomatchs = ['end']; function render($datas = array(), $templ = '', $nomatchs = array()) { //preg_replace_callback() //执行一个正则表达式搜索并且使用一个回调进行替换 return preg_replace_callback('/\\{([\w\-\/]+)\\}/', function ($matches) use ($datas, $nomatchs) { //$matches[1]返回如:username $name = $matches[1]; if (in_array($name, $nomatchs)) { //$mathches[0]返回如:{end} return $matches[0]; } return array_key_exists($name, $datas) ? $datas[$name] : ''; }, $templ); } var_dump(render($datas, $templ, $nomatchs)); //输出结果为:'我是个大帅比{end}'
相关免费学习推荐:php编程(视频)
以上就是php如何进行字符串匹配替换的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product