tags:

views:

631

answers:

3

How do I replace a single question mark with preg replace.

+7  A: 
preg_replace('/\?/', 'replacement', $original, 1)
chaos
+2  A: 

If it's a single character you're replacing, you may not need a preg_ solution: a "simple" str_replace may do the trick as well:

www.php.net/str_replace

Bart Kiers
A: 

If you want to make sure not to replace either question mark in the string testing ?? you could do:

// using negative lookbehind/ahead to ensure that the question mark 
// doesn't have a "friend"
$new = preg_replace('/(?<!\?)\?(?!\?)/', 'replacement', $original);

If you are looking to only replace the first question mark in the string - chaos's answer is what you want

gnarf