How can I replace the obsolete split PHP command with explode in a folder structure with many files?


In the meantime, I figured it out myself.

find . -iname '*.php' -type f -exec grep -l -m 1 -P '[<][?]' {} \; | xargs -r grep -l -m 1 --null -P '[^_.]split[\s]*[(]' | sudo xargs -0 -r perl -pi -e 's/([^_.])split([\s]*[(])/$1explode$2/g'
  1. Get PHP files (case-insensitive).
  2. Check if <? is inside.
  3. Check if split( is inside.
  4. Replace split( with explode(.

For 3 and 4: _split( (preg_split) and .split( (Javascript in my case) won't be replaced.

Tested on Ubuntu 18.04.

  • 1
    Refactoring code is generally best left to IDEs
    – jordanm
    Dec 27 '19 at 21:18
  • 2
    I upvoted your comment. But finally understanding all this find, grep, xargs, sed/perl, pipe and regex stuff makes me happy. :) It was a long run. :)
    – uav
    Dec 27 '19 at 23:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.