0

I have the following long string:

$long_text = "aaaaaaaaaaaaaaaaaaaaaaaa[BS][BS][BS][BS][BS][BS][BS][BS]aaaaaaaaaaaaaaaaaaaaaaaa[BS][BS][BS][BS][BS][BS][BS][BS]aaaaaaaaaaaaaaaaaaaaaaaa[BS][BS][BS][BS][BS][BS][BS][BS]aaaaaaaaaaaaaaaaaaaaaaaa[BS][BS][BS][BS][BS][BS][BS][BS]";

And I would like use [BS] as backspace. So I have the following code:

$long_text = preg_replace('/.(?R)*\[BS\]/', '', $long_text);

But It doesn't work because the string is too long.

Can someone help me and tell me why can't PHP handle this long text? Is there any way to handle it? (it works with shorter texts)

szakadars2
  • 23
  • 1
  • I seriously doubt it's a length matter. See [this question](http://stackoverflow.com/a/11770572/1679537) and [this](http://php.net/manual/fr/function.preg-replace.php#84285) – xlecoustillier Feb 03 '15 at 14:10
  • There is something wrong with your `.(?R)*`. If you leave that out it works fine. So check that. – putvande Feb 03 '15 at 14:11
  • Could easily be due to the backtracking limit. Why did you make that a recursive pattern? Make a less abstract example to explain how you came to that regex. – mario Feb 03 '15 at 14:13

1 Answers1

2

Not an answer to your question, but maybe to your problem ....

<?php
$s = "aaaaaaaaXbbbbbbbb[BS][BS][BS][BS][BS][BS][BS][BS]aaaaaaaaXbbbbbbbb[BS][BS][BS][BS][BS][BS][BS][BS]aaaaaaaaXbbbbbbbb[BS][BS][BS][BS][BS][BS][BS][BS]aaaaaaaaXbbbbbbbb[BS][BS][BS][BS][BS][BS][BS][BS]aaaaaaaa";

while ( false!==($pos=strpos($s, '[BS]')) ) {
    $s = substr_replace($s, '', $pos-1, 5);
}

echo $s;

prints

aaaaaaaaXaaaaaaaaXaaaaaaaaXaaaaaaaaXaaaaaaaa

(as intended)

VolkerK
  • 95,432
  • 20
  • 163
  • 226