php - remove parentheses from array -
i want remove parentheses if in beginning , end of given sting :
example :
$test = array("(hello world)", "hello (world)");
becomes :
$test = array("hello world", "hello (world)");
try this, using array_map()
anonymous function, , preg_replace()
:
$test = array("(hello world)", "hello (world)"); $test = array_map(function($item) { return preg_replace('/^\((.*)\)$/', '\1', $item); }, $test);
for example:
php > $test = array("(hello world)", "hello (world)"); php > $test = array_map(function($item) { return preg_replace('/^\((.*)\)$/', '\1', $item); }, $test); php > var_dump($test); array(2) { [0]=> string(11) "hello world" [1]=> string(13) "hello (world)" } php >
as @revo pointed out in comments, can modify array in place increase performance , reduce memory usage:
array_walk($test, function(&$value) { $value = preg_replace('/^\((.*)\)$/', '$1', $value); });
Comments
Post a Comment