First of all, you may want to learn about double and single enquoted strings in PHP.
Then your regex is wrongly formatted and is not what you want as well.
/\(|\)/|\$name/
Your regex has a syntax error with the slash )/|
before the pipe and after the closing brackets. It is unescaped and is also the escape character for your regex. (Meaning it is also present at the start and end of your regex.) You need to remove it first.
You will get a regex that looks like this /\(|\)|\$name/
. With it your function call has the following logic:
- Remove all opening brackets
- OR remove all closing brackets
- OR remove the string
$name
That is clearly not what you want. You want to remove all occurances of the word in your $name
variable, that are enclosed by brackets.
This can be achieved by the following function call:
$newline = preg_replace("/\($name\)/", '', $line);
If you want to make the brackets optional, use question mark operator to indicate, that they are not mandatory:
$newline = preg_replace("/\(?$name\)?/", '', $line);