Issue
Well I have a project that was made upgrade from php 7.4 to 8.1
so many functions in someway doesn't work as before because in php 8.1 doesn't accept null anymore.
So I want to replace all functions with a script so I could automation it.
Example:
line not affected before
explode(self::RULE_KEY_SEPARATOR, $key); somethingAfter
testeBefore explode(somthing, $key); somethingAfter
line not affected after
Output:
line not affected before
explode(self::RULE_KEY_SEPARATOR, $key ?? ''); somethingAfter
testeBefore explode(something, $key ?? ''); somethingAfter
line not affected after
So a solution could be add ?? ''
before close the parentesis of the function
here a snippet of what I tried to do: https://sed.js.org/?snippet=xqo9gd
I don't know if this is possible with sed.
If someone could help. I would appreciate :)
EDIT:
if snippet isn't available I will let here my try and output
I tried:
's/^\(.*\?\?\).*explode\(.*\).*/explode(self::RULE_KEY_SEPARATOR, $key ?? '\'''\''\);/g'
Output:
line not affected before
explode(self::RULE_KEY_SEPARATOR, $key ?? '');
explode(self::RULE_KEY_SEPARATOR, $key ?? '');
line not affected after
As expected since is not dynamically...
Solution
Your current command has a few issues;
- You create a capture group but do not utilize it therefore losing all leading whitespace
- After
explode
, you are creating another capture group rather than matching the parenthesis literally - After the second capture group, you use a greedy regex to capture and discard the remainder of the line.
- You are hardcoding the replace value rather than using the values already present
You can try this sed
$ sed "s/explode([^)]*/& ?? ''/" input_file
line not affected before
explode(self::RULE_KEY_SEPARATOR, $key ?? ''); somethingAfter
testeBefore explode(somthing, $key ?? ''); somethingAfter
line not affected after
Answered By - sseLtaH Answer Checked By - David Marino (WPSolving Volunteer)