Reputation: 5362
I have a string:
"Hello, my name is blah blah (goodbye) (hello) (oops)"
How do I remove "(hello)" but leaving the other two bracketed words?
I'm doing this right now in PHP but it removes ALL occurrences of brackets and anything inside them. I want to target a specific word, then remove the word and the surrounding brackets.
$newName= trim(preg_replace('/\s*\([^)]*\)/', '', $name));
Upvotes: 0
Views: 260
Reputation: 10539
Don't use regex for such an easy operation
$newName = trim(str_replace("(hello)", "", $name));
in order to remove more values, you don't even need to use str_replace multiple times, just pass array to the first argument
$remove = array(
"(oops)",
"(hello)"
);
$newName = trim(str_replace($remove, "", $name));
Upvotes: 8
Reputation: 10303
You can try something like this:
$toDelete = array("(hello)", "(bye)");
$neName = trim(str_replace($toDelete, "", $name));
This will delete all (hello) and (bye)'s in the text. You can add as many as you like.
Upvotes: 1