views:

58

answers:

1

An interesting question about str_replace.

Here's the example:

$search  = array('A', 'B', 'C', 'D', 'E');
$replace = array('B', 'C', 'D', 'E', 'F');
$subject = 'A';
echo str_replace($search, $replace, $subject);

What if I want to replace '" into something else, say -. How do I do that?

The problem is I can't write something like this

$search  = array(''"', 'B', 'C', 'D', 'E');
$replace = array('-', 'C', 'D', 'E', 'F');
$subject = 'A';
echo str_replace($search, $replace, $subject);

Any solution?

+7  A: 
$search  = array('\'"', 'B', 'C', 'D', 'E');

Single-quoted strings have two escapes, \' for ' and \\ for \.

Matthew Flaschen
Oh, escape works in array also? Didn't know that. Thanks!
Eric Sim
@Eric Sim: You can escape characters in any string anywhere in PHP code.
animuson