我在一个文件里有一行 CurrentStatus=`svcs -a | grep $name | awk '{print $1}'`  
需要替换成CurrentStatus=`svcs -a | grep $name | awk \'{print $1}\'` 用preg_replace怎么写,或者有什么其他办法。

解决方案 »

  1.   

    数据只有这点样本str_replace就够了
      

  2.   

    不用preg_replace,
    addslashes($string)就可以了。
      

  3.   

    echo addslashes(file_get_contents($filename));
      

  4.   

    set_magic_quotes_runtime (1);
    file_put_contents($filename, file_get_contents($filename));
      

  5.   

    set_magic_quotes_runtime (1);//效果和addslashes一样echo addslashes(file_get_contents($filename));都是可以的。
    string addslashes ( string $str )
    返回字符串,该字符串为了数据库查询语句等的需要在某些字符前加上了反斜线。这些字符是单引号(')、双引号(")、反斜线(\)与 NUL(NULL 字符)。 单引号(')、双引号(")、反斜线(\)与 NUL(NULL 字符)都会在前面加上\
    如果仅仅需要替换单引号('),则使用str_repalce
    echo str_replace("'","\'",$str);