Hello!
There is a string $string = aaa!bbbb!ccccc!ddddd!eeeeee!ffffff!gggg!...и до бесконечности
I need to create a regular expression that changes an arbitrary exclamation point to X
The number of characters can be at least one million repetitions of exclamation marks. Therefore, such options are:

 preg_replace('/^[^!]+/i', 'X', $string); //меняем первый по счету preg_replace('/^([^!]+)(!)([^!])/i', 'X', $string); //меняем второй по счету preg_replace('/^([^!]+)(!)([^!])(!)([^!])/i', 'X', $string); //меняем третий по счету... 


...unsuitable
Tell me how to make such a pattern (regular expression per sample), so that by changing one digit, choose which one ! change. Roughly speaking: I put it somewhere 1, the first one changed, I put 2, the second one changed, etc.
Thank!

  • Need support for preg_replace, or does str_replace also work? - Let's say Pie
  • come on str_replace too))) But I was interested in the ability to search and replace by regular expression - OO
  • see the answer. - Let's say Pie

3 answers 3

 $text = "aaa!bbbb!ccccc!ddddd!eeeeee!ffffff!gggg!"; $number = 2; $replace = preg_replace ('/!/e',"\$number--==1?'X':'\\0'", $text); echo $replace; 

And with a callback for those who wish -.-

 $text = "aaa!bbbb!ccccc!ddddd!eeeeee!ffffff!gggg!"; $number = 3; $replace = preg_replace_callback('/!/', function ($match) use (&$number) {return $number--==1?'X':$match[0];}, $text); echo $replace; 

Result:

 aaa!bbbb!cccccXddddd!eeeeee!ffffff!gggg! 
  • It seems to me that the e modifier is not used now, and the function is replaced by preg_replace_callback - Let's say Pie
  • @ Let'ssayPie -.- why you don’t please e I don’t know, but it can and is rarely used, but now it fits perfectly here. preg_replace_callback - will be much slower. - Manitikyl
  • I never liked it, but I put a plus: D upd: always with e warning, I like clean, xd) - Let's say Pie

If I understand correctly, then you want to replace the n-th found element in the line by the pattern, then here's an example:

 $string = 'aaa!bbbb!ccccc!ddddd!eeeeee!ffffff!gggg!'; echo preg_replace_n('/!/', 'X', $string, 2); // 2 - второе вхождение function preg_replace_n($search, $replace, $string, $counter) { $string = preg_replace_callback($search, function ($m) use (&$counter, $replace) { if ($counter-- == 1) return $replace; return $m[0]; }, $string); return $string; } 

At the output we get:

 aaa!bbbbXccccc!ddddd!eeeeee!ffffff!gggg! 
  • Uhhhh .... There is also the input line left to podtyunit so that '/' does not exist, a couple of checks on the input /, and you can on the github - Manitikyl
  • one
    @Manitikyl, are you about preg_quote? - Let's say Pie
  • Indeed, yes about him - Manitikyl
  • @Manitikyl, I’m not worthy of a githuba yet) only if pronjub))))) if you want, you can correct my answer with a check, I’ll accept) - Let's say Pie
  • That lan, so is also normal, there is already a question. Just change the first parameter to '/'.preg_quote($search).'/' . '/'.preg_quote($search).'/' as a thread like that. - Manitikyl

This is solved with preg_replace_callback ()

(preg_replace () with the e modifier in recent php versions removed!) :

 $str = 'aaa!bbbb!ccccc!ddddd!eeeeee!ffffff!gggg!'; $need = 1; echo preg_replace_callback( '~\!~', function($m)use($need){ static $i = 0; return ++$i == $need ? 'X' : $m[0]; }, $str ); 

Result:

 aaaXbbbb!ccccc!ddddd!eeeeee!ffffff!gggg!