我正在运行一个 Ubuntu EC2 实例。我经常使用查找和替换来更新和调整所有文件中使用的内容。
但是,当我必须替换PHP
代码时,我不可避免地必须替换美元符号,但这对我来说从未成功过。我已经学习并尝试了所有我所知的转义方法。我相信我有一种转义美元符号的方法,它实际上会用所需的文本替换文本,我尝试了替换;结果,我所有包含美元符号的文件(即所有文件)现在都被完全撕裂了,并充满了我试图替换的内容。
我使用的命令是:
find ./ -name \*.php -exec sed -i "s|[\'REQUEST_URI\']|\$_SERVER[\'REQUEST_URI\']|g" {} \;
我不知道为什么会发生这种情况,现在我创建了一年多的网站已经完全被毁了。
影响如下:
<?php
$sql =
"$_SERVER['REQUEST_URI']$_SERVER['REQUEST_URI']L$_SERVER['REQUEST_URI']C$_SERVER['REQUEST_URI'] questions.question$_SERVER['REQUEST_URI']title, questions.question$_SERVER['REQUEST_URI']content, questions.question$_SERVER['REQUEST_URI']date, questions.question$_SERVER['REQUEST_URI']answered
F$_SERVER['REQUEST_URI']OM questions
WH$_SERVER['REQUEST_URI']$_SERVER['REQUEST_URI']$_SERVER['REQUEST_URI'] questions.question$_SERVER['REQUEST_URI']topic = $topic$_SERVER['REQUEST_URI']D AND questions.question$_SERVER['REQUEST_URI']answered $_SERVER['REQUEST_URI']$_SERVER['REQUEST_URI'] N$_SERVER['REQUEST_URI']LL
O$_SERVER['REQUEST_URI']D$_SERVER['REQUEST_URI']$_SERVER['REQUEST_URI'] BY questions.question$_SERVER['REQUEST_URI']date D$_SERVER['REQUEST_URI']$_SERVER['REQUEST_URI']C
L$_SERVER['REQUEST_URI']M$_SERVER['REQUEST_URI']$_SERVER['REQUEST_URI'] 5";
$result = $conn->query($sql);
if($result !== N$_SERVER['REQUEST_URI']LL && $result->num$_SERVER['REQUEST_URI']rows > 0)
while($row = $result->fetch$_SERVER['REQUEST_URI']assoc()) {
echo "
<a href=$_SERVER['REQUEST_URI']view$_SERVER['REQUEST_URI']uestion.php?" . $$_SERVER['REQUEST_URI']$_SERVER['REQUEST_URI']$_SERVER['REQUEST_URI']$_SERVER['REQUEST_URI']V$_SERVER['REQUEST_URI']$_SERVER['REQUEST_URI'][$_SERVER['REQUEST_URI']$_SERVER['REQUEST_URI']$_SERVER['REQUEST_URI']$_SERVER['REQUEST_URI']$_SERVER['REQUEST_URI']Y$_SERVER['REQUEST_URI']$_SERVER['REQUEST_URI']$_SERVER['REQUEST_URI']$_SERVER['REQUEST_URI']$_SERVER['REQUEST_URI']NG$_SERVER['REQUEST_URI']] . "&question=" . $row[$_SERVER['REQUEST_URI']question$_SERVER['REQUEST_URI']title$_SERVER['REQUEST_URI']] . "$_SERVER['REQUEST_URI'] class=$_SERVER['REQUEST_URI']questionAnchor$_SERVER['REQUEST_URI']><div id=$_SERVER['REQUEST_URI']unanswered$_SERVER['REQUEST_URI']>
<div class=$_SERVER['REQUEST_URI']offset$_SERVER['REQUEST_URI']uestion$_SERVER['REQUEST_URI']>
<h4 class=$_SERVER['REQUEST_URI']thumbnail$_SERVER['REQUEST_URI']itle$_SERVER['REQUEST_URI']>" . $row[$_SERVER['REQUEST_URI']question$_SERVER['REQUEST_URI']title$_SERVER['REQUEST_URI']] . "</h4>
<div class=$_SERVER['REQUEST_URI']thumbnailDesc$_SERVER['REQUEST_URI']>" . $row[$_SERVER['REQUEST_URI']question$_SERVER['REQUEST_URI']content$_SERVER['REQUEST_URI']] . "</div>
</div>
</div></a>
";
答案1
我担心您的网站遭到了严重损坏。
分析您的sed
命令:
sed -i "s|[\'REQUEST_URI\']|\$_SERVER[\'REQUEST_URI\']|g"
该命令的效果是将'REQUST_I
集合中的任何字符(即每个单个'
, R
, , ...)替换为其找到的每个文件中的E
字符串。$_SERVER['REQUEST_URI']
find
这是不可逆的,因为任何实例$_SERVER['REQUEST_URI']
现在可能对应于集合中的任何字符'REQUST_I
。
唯一有点帮助的就是运行一个命令,将$_SERVER['REQUEST_URI']
受该命令影响的每个文件中的每个实例替换为单个字符,以提高可读性并减少修复文件的痛苦。
例如将 的每个实例替换$_SERVER['REQUEST_URI']
为点(这显然也会替换 的合法实例$_SERVER['REQUEST_URI']
):
sed "s/\$_SERVER\['REQUEST_URI'\]/./g" file
如果我在你的代码片段上运行命令,我会得到以下结果:
<?php
$sql =
"..L.C. questions.question.title, questions.question.content, questions.question.date, questions.question.answered
F.OM questions
WH... questions.question.topic = $topic.D AND questions.question.answered .. N.LL
O.D.. BY questions.question.date D..C
L.M.. 5";
$result = $conn->query($sql);
if($result !== N.LL && $result->num.rows > 0)
while($row = $result->fetch.assoc()) {
echo "
<a href=.view.uestion.php?" . $....V..[.....Y.....NG.] . "&question=" . $row[.question.title.] . ". class=.questionAnchor.><div id=.unanswered.>
<div class=.offset.uestion.>
<h4 class=.thumbnail.itle.>" . $row[.question.title.] . "</h4>
<div class=.thumbnailDesc.>" . $row[.question.content.] . "</div>
</div>
</div></a>
";