$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.