How to add ()&*<>! to a PHP regex pattern?

I am using the following line to correct a string:

$dbname =preg_replace("/[^A-Za-z0-9 _-]/", '', $dbArr[0]);

However I have a few names with &!().
At the moment these special characters are removed from the string.
I would like to at least add &!() but if possible ()&*<>! to the above pattern,
but I get errors when I add them.
Can someone help me how to add these special characters ?

Some of the characters have meaning in the preg/regex syntax and need to be escaped when used inside a pattern.

There’s a function that excapes raw data

https://www.php.net/manual/en/function.preg-quote.php

Thanks PHDR and CHORN, I still don’t understand this regex pattern idea very much, but your answers helped me figure out how to solve my problem.

I just added / before every special character I wanted in my original patern.
The special characters are now not removed anymore.

Old line:
$dbname =preg_replace("/[^A-Za-z0-9 _-]/", '', $dbArr[0]);

New line:
$dbname =preg_replace("/[^A-Za-z0-9 _-]\&\(\)\*\!/", '', $dbArr[0]);

==> &()*! <== was added to the old line.

What exactly does this mean and why is the string “incorrect” in the first place?

@bananamen
I am importing names from a CSV to a SQLite table.
I noticed several time, this didn’t work because there was only one or to much " or ’ .
So I managed to correct that with the above Regex function,
however these introduced the problem all characters other than a…z, A…Z, 0…9, space and _- was being ditched.
The names are for a phonebook, and e.g. Richard Chamberlain (Mobile) was being converted to: Richard rd Chamberlain Mobile (<== without the brackets).

PM me a sample of the CSV file with good and bad records

Sponsor our Newsletter | Privacy Policy | Terms of Service