preg_替换PHP文件内部

preg_替换PHP文件内部,php,preg-replace,Php,Preg Replace,我有一个正在解析的文件,我正在尝试替换$mail[“email\u from”]=”test@example.com";与$mail[“email_from”]=请求(“email”),(意思是我想替换开头有$mail[“email\u from”]的所有行,结尾有;),下面是我的preg\u replace: $email = "$mail[\"email_from\"] = request(\"email\")"; $newcontent = preg_replace("/\$mail[\"

我有一个正在解析的文件,我正在尝试替换
$mail[“email\u from”]=”test@example.com";
$mail[“email_from”]=请求(“email”)
,(意思是我想替换开头有
$mail[“email\u from”]
的所有行,结尾有
),下面是我的preg\u replace:

$email = "$mail[\"email_from\"] = request(\"email\")";
$newcontent = preg_replace("/\$mail[\"email_from\"](.+);/",$email,$content); 
我的代码有什么错误?如何修复它?非常感谢

你可能需要

$email = "\$mail[\"email_from\"] = request(\"email\")";
          ^--escape the $
你可能需要

$email = "\$mail[\"email_from\"] = request(\"email\")";
          ^--escape the $
你可能需要

$email = "\$mail[\"email_from\"] = request(\"email\")";
          ^--escape the $
你可能需要

$email = "\$mail[\"email_from\"] = request(\"email\")";
          ^--escape the $

在使用正确的引号并转义所需的所有字符后,这将起作用:

$email='$mail[“email_from”]='test@example.com";';
$replacement='$mail[“email_from”]=请求(“email”);
$newContent=preg\u replace(“/\\$mail\\[\\”email\u from\\“\\]”(.+);/i“,$replacement,$email);
echo$newContent//$邮件[“email_from”]=请求(“电子邮件”);

在使用正确的引号并转义您需要的所有字符后,此操作将起作用:

$email='$mail[“email_from”]='test@example.com";';
$replacement='$mail[“email_from”]=请求(“email”);
$newContent=preg\u replace(“/\\$mail\\[\\”email\u from\\“\\]”(.+);/i“,$replacement,$email);
echo$newContent//$邮件[“email_from”]=请求(“电子邮件”);

在使用正确的引号并转义您需要的所有字符后,此操作将起作用:

$email='$mail[“email_from”]='test@example.com";';
$replacement='$mail[“email_from”]=请求(“email”);
$newContent=preg\u replace(“/\\$mail\\[\\”email\u from\\“\\]”(.+);/i“,$replacement,$email);
echo$newContent//$邮件[“email_from”]=请求(“电子邮件”);

在使用正确的引号并转义您需要的所有字符后,此操作将起作用:

$email='$mail[“email_from”]='test@example.com";';
$replacement='$mail[“email_from”]=请求(“email”);
$newContent=preg\u replace(“/\\$mail\\[\\”email\u from\\“\\]”(.+);/i“,$replacement,$email);
echo$newContent//$邮件[“email_from”]=请求(“电子邮件”);

使用
^
$
指定行的开头和结尾。像
$
[
]
这样的特殊字符需要转义

<?php
$content = '$mail["email_from"] = "test@example.com";';
$email = '$mail["email_from"] = request("email");';
$newcontent = preg_replace('/^\$mail\["email_from"\] =.+;$/',$email,$content); 

echo $newcontent . "\n";

使用
^
$
指定行的开头和结尾。像
$
[
]
这样的特殊字符需要转义

<?php
$content = '$mail["email_from"] = "test@example.com";';
$email = '$mail["email_from"] = request("email");';
$newcontent = preg_replace('/^\$mail\["email_from"\] =.+;$/',$email,$content); 

echo $newcontent . "\n";

使用
^
$
指定行的开头和结尾。像
$
[
]
这样的特殊字符需要转义

<?php
$content = '$mail["email_from"] = "test@example.com";';
$email = '$mail["email_from"] = request("email");';
$newcontent = preg_replace('/^\$mail\["email_from"\] =.+;$/',$email,$content); 

echo $newcontent . "\n";

使用
^
$
指定行的开头和结尾。像
$
[
]
这样的特殊字符需要转义

<?php
$content = '$mail["email_from"] = "test@example.com";';
$email = '$mail["email_from"] = request("email");';
$newcontent = preg_replace('/^\$mail\["email_from"\] =.+;$/',$email,$content); 

echo $newcontent . "\n";


这有意义吗?“$mail”被解释为使用双引号。使用简单的引号代替,这有意义吗?“$mail”被解释为使用双引号。使用简单的引号代替,这有意义吗?“$mail”被解释为使用双引号。使用简单的引号代替,这有意义吗?“$mail”被解释为使用双引号。使用简单的引语代替标题,我想这也和正则表达式有关!这里有一个演示:你对正则表达式太过分了。用一个简单的str_replace()比用正则表达式简单得多。我试过了,我想这也和正则表达式有关!这里有一个演示:你对正则表达式太过分了。用一个简单的str_replace()比用正则表达式简单得多。我试过了,我想这也和正则表达式有关!这里有一个演示:你对正则表达式太过分了。用一个简单的str_replace()比用正则表达式简单得多。我试过了,我想这也和正则表达式有关!这里有一个演示:你对正则表达式太过分了。使用简单的str_replace()比使用正则表达式容易得多。请查看演示!你能试试我的答案吗?它按预期工作。看到了吗:我问的可能完全错了,但你为什么要打印这些文本?@PrashantBalan我也不知道,但可能要将其存储在PHP文件中,稍后再使用,我想你能检查一下演示吗!你能试试我的答案吗?它按预期工作。看到了吗:我问的可能完全错了,但你为什么要打印这些文本?@PrashantBalan我也不知道,但可能要将其存储在PHP文件中,稍后再使用,我想你能检查一下演示吗!你能试试我的答案吗?它按预期工作。看到了吗:我问的可能完全错了,但你为什么要打印这些文本?@PrashantBalan我也不知道,但可能要将其存储在PHP文件中,稍后再使用,我想你能检查一下演示吗!你能试试我的答案吗?它按预期工作。看到了吧:我问的可能完全错了,但你为什么要打印文本?@PrashantBalan我也不知道,但我想可能是把它存储在一个PHP文件中,以后再使用