请高手解决php正则替换 把字符串 abcdefghi中的 a c gh 前后用 <>起来结果用该是 <a>b<c>def<gh>i这样的必须用preg_replace。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 preg_replace(array("/a/", "/c/", "/gh/"), array("<a>","<c>","<gh>"), "abcdefghi"); function highlight($content,$key){ $tmpkey = explode(" ",$key); $len = count($tmpkey); $a1 = ""; $a2 = ""; for ($i=0;$i<$len;$i++){ if($tmpkey[$i] <>""){ $a1[$i] = $tmpkey[$i]; $a2[$i] = "<font color=red>".$tmpkey[$i]."</font>"; } } $content = preg_replace($a1, $a2 ,$content); return $content;}echo highlight("abcdefghijk","a c f ij");jianye112 帮我看下这么写对吗? $string = "abcdefghi";$patterns[0] = "/a/";$patterns[1] = "/c/";$patterns[2] = "/gh/";$replacements[2] = "<a>";$replacements[1] = "<b>";$replacements[0] = "<gh>";print preg_replace($patterns, $replacements, $string); replace(/(a|b|gh)/g, "<$&>");这是js的替换,现在有事没开有php环境的电脑,没有测试php的 preg_replace(array('/a/','/c/','/gh/'),array('<a>','<c>','<gh>'),$string); Regex.Replace("abcdefghi","a|c|gh","<$&>") $string = "abcdefghi";$pattern = "/(a|c|gh)/i";$replacement = "\${1}>";print preg_replace($pattern, $replacement, $string);只能做成这样,等等高手! preg_replace(array('/a/','/c/','/gh/'),array('<a>','<c>','<gh>'),$str); echo preg_replace("/a|c|gh/", "<$0>", "abcdefghi");out:<a>b<c>def<gh>i 42次月榜第一,楼上你太猛了。还是php人气太低了? $string = "abcdefghi";$pattern = "/(a|c|gh)/i";$replacement = "<\${1}>";echo preg_replace($pattern, $replacement, $string);输出是:bdefi但查看源代码是:<a>b<c>def<gh>i可能是浏览器翻译的缘故吧 求救!!PHP怎么把数据库的多个图片显示出来 验证图片出不来请求助 phpMyAdmin错误:无法载入 mysql 扩展,请检查 PHP 配置 求助 PHP 求图片大小异常 PHP中插入声音的问题 统计字符长度 谁知道ZDE怎样才能支持中文! 啥问题造成的呢? 请教:为什么进入.php页面弹出下载对话框?谢谢! 数据取整!! 关于 mysqli 使用的问题
$tmpkey = explode(" ",$key);
$len = count($tmpkey);
$a1 = "";
$a2 = "";
for ($i=0;$i<$len;$i++){
if($tmpkey[$i] <>""){
$a1[$i] = $tmpkey[$i];
$a2[$i] = "<font color=red>".$tmpkey[$i]."</font>";
}
}
$content = preg_replace($a1, $a2 ,$content);
return $content;
}echo highlight("abcdefghijk","a c f ij");
jianye112 帮我看下这么写对吗?
$string = "abcdefghi";$patterns[0] = "/a/";
$patterns[1] = "/c/";
$patterns[2] = "/gh/";$replacements[2] = "<a>";
$replacements[1] = "<b>";
$replacements[0] = "<gh>";print preg_replace($patterns, $replacements, $string);
这是js的替换,现在有事没开有php环境的电脑,没有测试php的
$pattern = "/(a|c|gh)/i";
$replacement = "\${1}>";
print preg_replace($pattern, $replacement, $string);
只能做成这样,等等高手!
<a>b<c>def<gh>i
$replacement = "<\${1}>";
echo preg_replace($pattern, $replacement, $string);
输出是:bdefi
但查看源代码是:<a>b<c>def<gh>i
可能是浏览器翻译的缘故吧