0

My dev and production env doesn't have the same exact version

My Dev env : PHP 7.0.31-1+ubuntu14.04.1+deb.sury.org+1 (cli)

My prod env : PHP 7.0.30 (cli)

For the exact same text this regular expression

preg_match_all('/%occupancies%((?!%endoccupancies%).|\n)*%endoccupancies%/', $text, $matches);

find matches in my dev env but not in prod

Code simple :

http://sandbox.onlinephpfunctions.com/code/b4541decc65089269f6aae0e4392dc9081c14615

Is there a way to fix this without upgrading my php version ?

user2942945
  • 469
  • 1
  • 7
  • 19
  • Your assumption with the minor version being relevant is really farfetched. Look at the PCRE version for something meaningful. **After** you verified the $text input to be *identical* on both environments. That's a very cumbersome regex to achieve what you probably wanted. – mario Sep 24 '18 at 18:05
  • 2
    [You shouldn't parse HTML with regex](https://stackoverflow.com/a/1732454/5827005) – GrumpyCrouton Sep 24 '18 at 18:20
  • Yes, it is easy, use `'/%occupancies%[^%]*(?:%(?!endoccupancies%)[^%]*)*%endoccupancies%/'` – Wiktor Stribiżew Sep 24 '18 at 18:42

1 Answers1

0

Your pattern contains ((?!%endoccupancies%).|\n)* alternation group that matches either a char other than a line break char that does not start a %endoccupancies% substring or a newline char (so, it does not match CRLF endings, e.g.). This group is quantified with * and that means that each char matched with this group is saved in the Group 1 memory slot, and is re-written each time a char is captured. That creates a huge overhead for the PCRE engine.

Re-write that part with [^%]*(?:%(?!endoccupancies%)[^%]*)* pattern. It matches:

  • [^%]* - 0+ chars other than %
  • (?:%(?!endoccupancies%)[^%]*)* - 0 or more occurrences of
    • %(?!endoccupancies%) - a % not immediately followed with endoccupancies%
    • [^%]* - 0+ chars other than %
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563