【问题标题】:Exact string replacing with preg_replace() [closed]用 preg_replace() 替换确切的字符串 [关闭]
【发布时间】:2018-02-27 03:07:59
【问题描述】:

当我尝试替换此字符串时遇到问题:

str1 = "mysql_connect($dbhost.':'.$dbport, $dbusername, $dbuserpass);";

使用其他字符串:

str2 = "$con = mysqli_connect($dbhost,$dbusername,$dbuserpass,"normativa",$dbport);";

我试过用这个:

$text = str_ireplace($str1,$str2,$text);

问题似乎是 . (点)在第一个字符串中。我尝试用“。”转义它。但没有运气。

是否有任何其他转义方法可以让点或其他 php 函数完成相同的事情?

【问题讨论】:

  • 这里有多个问题。 str_ireplace 不需要正则表达式。双引号中的变量将是值,而不是变量。 mysql_ 函数与 mysqli 函数不同。
  • 你的 MySQL 连接代码在这样的字符串中,这有点可怕。这里出了点大问题。

标签: php regex string str-replace


【解决方案1】:

您正在使用双引号,它将每个$ 作为一个变量。 试试这个:

$text = str_replace('mysql_connect($dbhost.\':\'.$dbport, $dbusername, $dbuserpass);','$con = mysqli_connect($dbhost,$dbusername,$dbuserpass,"normativa",$dbport);', $text);

【讨论】:

  • 这个成功了!谢谢!
  • 非常欢迎。您可以选择我的答案作为已选答案
猜你喜欢
  • 1970-01-01
  • 2023-03-17
  • 2011-01-29
  • 2014-12-11
  • 2013-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多