$string = " Some string ";
//the output should look like this
$output = "___Some string__";
So each leading and trailing whitespace replaced by underscore.
I found the regex for this in C here: Replace only leading and trailing whitespace with underscore using regex in c#
but i couldn’t make it work in php.
You could use a replace like:
\Gmatches at the beginning of the string or at the end of the previous match,(?=\s*$)matches if the following is only whitespace at the end of the string.So this expression matches each of the spaces and replaces them with a
_.