ereg_replace
(PHP 3, PHP 4, PHP 5)ereg_replace -- 正则表达式替换
说明
string ereg_replace ( string pattern, string replacement, string string )
本函数在 string 中扫描与 pattern 匹配的部分,并将其替换为 replacement。 返回替换后的字符串。(如果没有可供替换的匹配项则会返回原字符串。) 如果 pattern 包含有括号内的子串,则 replacement 可以包含形如 \\digit 的子串,这些子串将被替换为数字表示的的第几个括号内的子串;\\0 则包含了字符串的整个内容。最多可以用九个子串。括号可以嵌套,此情形下以左圆括号来计算顺序。 如果未在 string 中找到匹配项,则 string 将原样返回。 例如,下面的代码片断输出 "This was a test" 三次: 例子 1. ereg_replace() 例子<?php
$string = "This is a test";
echo str_replace(" is", " was", $string);
echo ereg_replace("( )is", "\\1was", $string);
echo ereg_replace("(( )is)", "\\2was", $string);
?>  
 
要注意的一点是如果在 replacement 参数中使用了整数值,则可能得不到所期望的结果。这是因为 ereg_replace() 将把数字作为字符的序列值来解释并应用之。例如: 例子 2. ereg_replace() 例子<?php
/* 不能产生出期望的结果 */
$num = 4;
$string = "This string has four words.";
$string = ereg_replace('four', $num, $string);
echo $string;   /* Output: 'This string has   words.' *//* 本例工作正常 */
$num = '4';
$string = "This string has four words.";
$string = ereg_replace('four', $num, $string);
echo $string;   /* Output: 'This string has 4 words.' */
?>  
 
例子 3. 将 URL 替换为超连接<?php
$text = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]",
                     "<a href=\"\\0\">\\0</a>", $text);
?> 
 

解决方案 »

  1.   

    $str='asdfghhhhfeee$qqq$jkjkljljlj hfjdskahf $ppp$fhdaskfhjj';
    $ar=explode('$',$str);
    $str="";
    for($i=0;$i<count($ar);){
        $str.=$ar[$i];
        $i+=2;
    }
    echo $str;
      

  2.   

    这种分割 与 正则匹配 哪个要好些,效率高些?我使用:
    $modden="^\$[a-zA-Z_]+\$$";可是我始终不能 使$作为结束符。
      

  3.   

    $str='asdfghhhhfeee$qqq$jkjkljljlj hfjdskahf $ppp$fhdaskfhjj';
    echo preg_replace('/\$(.*?)\$/is','',$str);
      

  4.   

    能解释一下preg_replace('/\$(.*?)\$/is','',$str);中的正则匹配吗
      

  5.   

    preg_replace('/\$(.*?)\$/is','',$str);'/' is the start and end tagthe sub-string that will be replaced is started by '$' (\$),notice:here is not the start of a string,so it should not be "^".
    then it include some characters or whitespace characters(.*)
    and we want to replace all of the substrings(.*?)
    finally the end '$'(\$)
      

  6.   

    you can learn how to use regular expression in php manual.
    if you have questions,you can use google to learn more.
      

  7.   

    to zeroleonhart(Strong Point:Algorithm) :你的这个 preg_replace('/\$(.*?)\$/is','',$str); 也不能把$作为终止符。。
    结果和我
    $modden="^\$[a-zA-Z_]+\$$";  效果是一样的
      

  8.   

    $str='asdfghhhhfeee$qqq$jkjkljljlj hfjdskahf $ppp$fhdaskfhjj';
    echo preg_replace('/\$(.*?)\$/is','',$str)."<br>";//mine
    echo preg_replace('/^\$[a-zA-Z_]+\$$/','',$str);//yours
    the result:asdfghhhhfeeejkjkljljlj hfjdskahf fhdaskfhjj
    asdfghhhhfeee$qqq$jkjkljljlj hfjdskahf $ppp$fhdaskfhjj
      

  9.   

    我明白了
    我那个 $str后面的字符是“”(双引号) 。你的是‘’(单引号)
    我想问一下这样有什么不同吗?
    你要用“”(双引号) ,保证不正确。这其中有什么原因吗??
      

  10.   

    zeroleonhart(Strong Point:Algorithm) ( ) 信誉:100 
    正解
      

  11.   

    是啊,
    zeroleonhart(Strong Point:Algorithm) ( ) 信誉:100 
    是对的。
    但是我说的
    =====================================
    我那个 $str后面的字符是“”(双引号) 。你的是‘’(单引号)
    我想问一下这样有什么不同吗?
    你要用“”(双引号) ,保证不正确。这其中有什么原因吗??
    =====================================这个我还不明白啊??
      

  12.   

    if you use "\$a",PHP will think it is '\' and a variable $a,not string '\$a'.
    when you use '$' in your re,you'd better use '' .
      

  13.   

    sorry,if you use "\$a",PHP will think it is equal to '$a' and  re will think the '$' in it is the ending tag '$'. for example:
    $a=3;
    echo "\$a"."<br>".$a."<br>".'\$a';it will output:
    $a
    3
    \$a
      

  14.   

    to zeroleonhart(Strong Point:Algorithm) :
    can you  see:
    http://community.csdn.net/Expert/topic/4889/4889507.xml?temp=.2494013thanks!
      

  15.   

    如果 :$str="asdfghhhhfeee$qqq$jkjkljljlj hfjdskahf $ppp$fhdaskfhjj";$str里的$...都将被解析成为变量。单引号的不进行解析。