【问题标题】:Limit replacement times限制更换时间
【发布时间】:2011-06-21 12:02:35
【问题描述】:

PHP 中是否有一个函数可以用来将特定字符替换为另一个字符,但我应该能够限制完成的替换次数? 我目前正试图让 preg_replace() 工作,因为它具有这种能力,但似乎有问题。这是我正在使用的:

$args[0] = preg_replace("/\$/", $args[$x], $args[0], 1);

$args[0] contains letters and special "$" characters, which should be replaced.

有什么帮助吗?

【问题讨论】:

  • 你能把$args[$x]$args[0]的值作为例子吗?

标签: php regex


【解决方案1】:

你试过"/\\$/"(2 个反斜杠)吗?

(请参阅How to escape $ in PHP using preg_replace? 了解更多信息)

【讨论】:

    【解决方案2】:

    这是替换 fn 的语法:

    preg_replace($patterns, $replacements, $string);
    

    所以你必须先把模式放在字符串末尾,然后是替换(你想用它替换的字符)。

    【讨论】:

    • 第四个参数(可选)指定执行替换的次数限制。感谢您愿意发帖,不过看来函数的用法已经了解了。
    猜你喜欢
    • 2019-07-02
    • 2010-11-17
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-17
    • 1970-01-01
    相关资源
    最近更新 更多