Php: preg replace one question mark?

How to replace one question mark with preg.

+2


source to share


3 answers


preg_replace('/\?/', 'replacement', $original, 1)

      



+9


source


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



www.php.net/str_replace

+2


source


If you wanted not to replace the 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 only want to replace the first question mark in the line - chaos request is what you want

0


source







All Articles