In the following string, how can I remove the spaces inside the parentheses?
"The quick brown fox (jumps over the lazy dog)"
"The quick brown fox (jumpsoverthelazydog)"
preg_replace("/\(.*?\)/", "", $string)
preg_replace("/\(\s\)/", "", $string)
"The quick brown fox (jumps over the lazy dog)"
"The quick (brown fox jumps) over (the lazy dog)"
"(The quick brown fox) jumps over the lazy dog"
function empty_parantheses($string) {
return preg_replace_callback("<\(.*?\)>", function($match) {
return preg_replace("<\s*>", "", $match[0]);
}, $string);
}
The easiest work-around would be to use preg_replace()
within preg_replace_callback()
without any looping or separate replace-functions
as shown below. The advantage is that you could have even more than one group of strings wrapped inside (parenthesis) as the example below shows.. and, by the way, you may test it out here.
<?php
$str = "The quick brown fox (jumps over the lazy dog) and (the fiery lion caught it)";
$str = preg_replace_callback("#\(.*?\)#", function($match) {
$noSpace = preg_replace("#\s*?#", "", $match[0]);
return $noSpace;
}, $str);
var_dump($str);
// PRODUCES:: The quick brown fox (jumpsoverthelazydog) and (thefierylioncaughtit)' (length=68)