Extract string between first occurring set of parenthesis I have this function and need to refactor it
Please refactor the code below to make it more concise, efficient and readable with good logic flow
function findFirstStringInBracket($str){
    if(strlen($str) &gt; 0){
        $firstbracket = strstr($str, '(');
        if($firstbracket){
            $firstbracket = ltrim($firstbracket, '(');
            return strstr($firstbracket, ')', true);
        }else{
            return '';
        }
    }else{
        return '';
    }
}
I find my snippet below to be direct, concise, and readable (but I rarely have any trouble reading regex patterns).
The pattern literally matches the first occurring opening parenthesis, then captures one or more non-closing-parenthesis characters, then matches the required closing parenthesis.
A regex approach means a single function call solution and only one return in your custom function (which makes your code cleaner to read).
Other devs may prefer to avoid regex, but I think this is a perfect case for employing it.
One note of difference between my script and yours, is that if the closing parenthesis is missing in the input string with your script, your function will return false.  My script will always return a string.  (see here)
Code:
function findFirstStringInBracket(string $string): string
{
    return preg_match('~\(([^)]+)\)~', $string, $match) ? $match[1] : '';
}

Unless you are doing hundreds of thousands of iterations of this call, performance probably isn't an issue.  If it is a bottle neck, then you probably want to look at other ways to improve script performance.
(Demo with some test cases)
$strings = [
    'the(crow)bar',
    'foo()bar',
    'handlebar()',
    '()foofighters',
    '(foobar)',
    '()',
    'barfood',
    'barf(oo',
    'fooeybar)',
    ')foob(',
];

$result = [];
foreach ($strings as $string) {
    $result[$string] = preg_match('~\(([^)]+)\)~', $string, $match) ? $match[1] : '';
}
var_export($result);

Output:
array (
  'the(crow)bar' =&gt; 'crow',
  'foo()bar' =&gt; '',
  'handlebar()' =&gt; '',
  '()foofighters' =&gt; '',
  '(foobar)' =&gt; 'foobar',
  '()' =&gt; '',
  'barfood' =&gt; '',
  'barf(oo' =&gt; '',
  'fooeybar)' =&gt; '',
  ')foob(' =&gt; '',
)