【问题标题】:preg_replace() not replacing with <. Returns failurepreg_replace() 不替换为 <。退货失败
【发布时间】:2014-06-06 16:04:44
【问题描述】:

我正在尝试使用preg_replace\u003C 替换为&lt;,但它返回相同的输入。

这是我的 preg_replace

 preg_replace('/\\\\u003C/','<','name\u003Cname@example.com\u003E');

任何帮助。

【问题讨论】:

  • 为什么要在模式中加入四个斜线\\\\u003C?两个就够了。
  • @Pete_Gore:需要一对是因为您在 PHP 字符串文字中,另一对是因为您正在编写正则表达式。
  • 我测试了preg_replace('/\\u003C/','&lt;','name\u003Cname@example.com\u003E');,效果很好
  • 为什么 OP 不使用str_replace

标签: php regex preg-replace


【解决方案1】:

如果你想替换一个特定的字符串,你应该使用:

str_replace("\u003C", "<", "name\u003Cname@example.com\u003E");

我认为你不需要正则表达式。

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2014-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-07
    • 1970-01-01
    相关资源
    最近更新 更多