2

is there a function in PHP that I can use to replace a specific character with another one, but I should be able to limit the number of replacements done? I am currently trying to get preg_replace() to work, as it has that ability, but there seems to be something wrong. Here is what I am using:

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

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

Any help?

Naveed
  • 41,517
  • 32
  • 98
  • 131

2 Answers2

5

Have you tried "/\\$/" (2 backslashes)?

(See How to escape $ in PHP using preg_replace? for more info)

Community
  • 1
  • 1
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
-1

This is the syntax of replacement fn:

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

so you have to put first the patterns after that the replacements (the char you want to replace with it) at the end of your string.

ghufran
  • 1
  • 1
  • And the fourth argument (which is optional) specifies a limit to the number of times the replacement is performed. Thanks for your willingness to post, but it appears that the function's usage is already understood. – Wiseguy Jun 21 '11 at 12:11