把字符串 abcdefghi中的 a c gh 前后用 <>起来结果用该是 <a>b<c>def<gh>i
这样的必须用preg_replace。

解决方案 »

  1.   

    preg_replace(array("/a/", "/c/", "/gh/"), array("<a>","<c>","<gh>"), "abcdefghi");
      

  2.   

    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 帮我看下这么写对吗?
      

  3.   


     $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);
      

  4.   

    replace(/(a|b|gh)/g, "<$&>");
    这是js的替换,现在有事没开有php环境的电脑,没有测试php的
      

  5.   

    preg_replace(array('/a/','/c/','/gh/'),array('<a>','<c>','<gh>'),$string);
      

  6.   

    Regex.Replace("abcdefghi","a|c|gh","<$&>")
      

  7.   

    $string = "abcdefghi";
    $pattern = "/(a|c|gh)/i";
    $replacement = "\${1}>";
    print preg_replace($pattern, $replacement, $string);
    只能做成这样,等等高手!
      

  8.   

    preg_replace(array('/a/','/c/','/gh/'),array('&lt;a&gt;','&lt;c&gt;','&lt;gh&gt;'),$str);
      

  9.   

    echo preg_replace("/a|c|gh/", "<$0>", "abcdefghi");out:
    <a>b<c>def<gh>i
      

  10.   

    42次月榜第一,楼上你太猛了。还是php人气太低了?
      

  11.   

    $string = "abcdefghi";$pattern = "/(a|c|gh)/i";
    $replacement = "<\${1}>";
    echo preg_replace($pattern, $replacement, $string);
    输出是:bdefi
    但查看源代码是:<a>b<c>def<gh>i
    可能是浏览器翻译的缘故吧