2

I want to remove parentheses only if they are in the beginning and the end of a given sting :

Example :

$test = array("(hello world)", "hello (world)");

becomes :

$test = array("hello world", "hello (world)");

1
  • 2
    Please edit your question to show what you have tried, and what issues that attempt had. Also, you need to fully define the problem - for example, do you need to match opening and closing braces? What happens if there are multiple braces ((hello) world), or mismatched (hello) world) ))hello() world() etc Commented Jul 11, 2016 at 13:29

4 Answers 4

4

Try this, using array_map() with an anonymous function, and 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 the comments, we can also modify the array in place to increase performance and reduce memory usage:

array_walk($test, function(&$value) {
    $value = preg_replace('/^\((.*)\)$/', '$1', $value);
});
Sign up to request clarification or add additional context in comments.

5 Comments

FYI Instead of doing a array_map() and storing modified cloned array to the same variable $test, you may use a array_walk() with a pass-by-reference argument.
I can't think of a good way to modify it in place without having the keys as well. We'd have to use array_walk() as theres no good way to pass the keys by reference.
I didn't get you thoroughly, but you don't need to pass keys at all.
Ahh, hmm, do you mean array_map(function($item) use (&$test) { $item = preg_replace(...); }, $test);?
What's its purpose?! That doesn't seem right to me. I just offered array_walk over array_map in your case: array_walk($test, function(&$value) { $value = preg_replace('#^\((.*)\)$#', '$1', $value); });
1

You can use preg_replace with array_map:

$test = array("(hello world)", "hello (world)");

$finalArr = array_map(function($value) {
    return preg_replace("/^\((.*)\)$/", "$1", $value);
}, $test);

print_r($finalArr);

Result:

Array
(
    [0] => hello world
    [1] => hello (world)
)

Remember: It will leave out, (hello world or hello world)

1 Comment

@Rizier123 No. Read the question again.
1

You could use regex:

For example

<?php
$test = array("(hello world)", "hello (world)");

foreach ($test as &$val) {
     if (preg_match("/^\(.*\)$/",$val)) {
        $val = substr($val,1,-1);
     }
}

print_r($test);

Prints:

Array ( [0] => hello world [1] => hello (world) )

Comments

0
<?php

// if first character = "(" AND last character = ")"
if (substr($string, 0,1) == "(" && substr($string, 0,-1) == ")") 
{
    $string = substr($string, 1);
    $string = substr($string, 0,-1);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.