Roman Roman - 4 months ago 11x
PHP Question

How to replace occurence of a character in a string except the first one

What I am trying to do is find question marks in a string and replace it with

except the first one. My code so far:

$str = '';
echo preg_replace(array('/[?]/'), '&', $str );


How can I ignore the first occurence of the


Match the start of the string up to the first ? and use a (*SKIP)(*F) to omit the first match:

$str = '';        
echo preg_replace('/^[^?]*\?(*SKIP)(*F)|[?]/', '&', $str );
// =>

See the IDEONE demo

Pattern details:

  • ^ - start of a string
  • [^?]* - 0+ characters other than ? as many as possible
  • \? - a literal ?
  • (*SKIP)(*F) - two PCRE verbs making the regex engine omit the text matched so far in the current iteration
  • | - or
  • [?] - a literal ?

Alternative to (*SKIP)(*FAIL) is to use preg_replace_callback and the former pattern with the first alternative branch inside capturing parentheses:

$str = '';        
echo preg_replace_callback('/^([^?]*[?])|[?]/', function($m) {
    return !empty($m[1]) ? $m[1] : "&";
}, $str );

See this IDEONE demo

The ^([^?]*[?]) part matches the string part from the start will the first ? and places into Group 1. Inside the anonymous method where we pass the match object ($m), we can check if the group matched ("participated in the match") with the help of !empty($m[1]). If it is, we just put it back. If not, the [?], the second branch matched, so, we replace it.