【问题标题】:facing issue in preg_replace()preg_replace() 面临问题
【发布时间】:2012-01-29 18:16:33
【问题描述】:

我想用"hi" from "this is nice place. <br> hello world.<br>thanks and regards" 替换"hello world." 和之后的任何字符preg_replace()

代码是这样的

$arr1 = array('/hello world./');
$arr2 = array('hi');
$str = "this is nice place. <br> hello world.<br>thanks and regards.";
echo preg_replace($arr1, $arr2, $str);

但是有了这个,我不仅能够替换“hello world”。从 $str。但我希望正则表达式替换“hello world”之后的所有字符。还。

【问题讨论】:

  • 那么...问题是什么?您的代码(有点)有效。
  • 但是有了这个,我不仅能够替换“hello world”。从 $str。但我希望正则表达式替换“hello world”之后的所有字符。也。

标签: regex php-5.3


【解决方案1】:
$arr1 = array('/hello world\..*$/'); $arr2 = array('hi'); $str = "this is nice place. <br> hello world.<br>thanks and regards."; echo preg_replace($arr1, $arr2, $str);

【讨论】:

    【解决方案2】:
    $arr1 = array('/hello world\..*$/');
    

    第一个点被\ 转义,否则它是一个表示“任何字符”的运算符。以下是“任意字符”(.) 任意次数 (*) 后跟字符串结尾 ($)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-09
      • 2020-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多