$newstr = preg_replace('/[^a-zA-Z0-9\']/', '_', "There wouldn't be any");
$newstr = str_replace("'", '', $newstr);

I put them on two separate lines to make the code a little more clear.

Note: If you're looking for Unicode support, see Filip's answer below. It will match all characters that register as letters in addition to A-z.

Answer from Chris Bornhoft on Stack Overflow
🌐
PHP
php.net › manual › en › function.preg-replace.php
PHP: preg_replace - Manual
Be aware that when using the "/u" modifier, if your input text contains any bad UTF-8 code sequences, then preg_replace will return an empty string, regardless of whether there were any matches. This is due to the PCRE library returning an error code if the string contains bad UTF-8. ... From what I can see, the problem is, that if you go straight and substitute all 'A's wit 'T's you can't tell for sure which 'T's to substitute with 'A's afterwards. This can be for instance solved by simply replacing all 'A's by another character (for instance '_' or whatever you like), then replacing all 'T's
Discussions

preg_replace remove special characters - PHP Coding Help - PHP Freaks
Forgot your password · By jasonc310771 December 15, 2022 in PHP Coding Help More on forums.phpfreaks.com
🌐 forums.phpfreaks.com
December 15, 2022
How can I preg_replace with special characters, numbers and everything? - PHP - SitePoint Forums | Web Development & Design Community
How can I preg_replace with special characters, numbers and everything · BTW, if you want to use regular expressions, only the pattern is a regex; the replacement is a string where you can access captured matches like “$1” (say) · This topic was automatically closed 91 days after the ... More on sitepoint.com
🌐 sitepoint.com
0
August 8, 2016
preg_replace to allow special characters
Hello I am using following code to allow characters like A to Z and numeric values like 0 to 9. $result = preg_replace('/[^a-zA-Z0-9_]/', '',... More on forums.digitalpoint.com
🌐 forums.digitalpoint.com
php - Preg_replace special characters & - Stack Overflow
Good afternoon, I have an error in my preg_replace. I would like to replace && with only one &, and ?& with ?. My code looks like this: $reg = preg_replace("#\&\&#is", "... More on stackoverflow.com
🌐 stackoverflow.com
November 8, 2015
🌐
SitePoint
sitepoint.com › php
Help with regexes code for preg_replace - PHP - SitePoint Forums | Web Development & Design Community
December 7, 2022 - I have read some tutorials on the ... I want to create a code that will do these these things: Remove most special characters (all special characters except _ - &) Convert Ampersand (&) to “and” ......
🌐
PHP Freaks
forums.phpfreaks.com › php coding › php coding help
preg_replace remove special characters - PHP Coding Help - PHP Freaks
December 15, 2022 - $string = "t*e*s*t"; $pattern = "/[^A-Za-zÀ-ÿ0-9\-\_\(\)\[\]\{\} ]/"; // keep all language letters, numbers, all types of brackets, spaces, hyphen, dash fullstop $cleanStr = preg_replace($pattern, '', $string); echo("{" . $cleanStr . "}"); I do not understand why this is not working, apart from t...
🌐
SitePoint
sitepoint.com › php
How can I preg_replace with special characters, numbers and everything? - PHP - SitePoint Forums | Web Development & Design Community
August 8, 2016 - $pattern = ‘/^([0-9]{3})001([0-9]{0,})$/’; $replacement = ‘/^([0-9]{3})001([0-9]{0,})$/’; $subject = ‘CLP520001103480891775486.2**1202014282509832Z0X00221~ NM1QC1BALLWANDAMIR59915931~ NM1IL1BALLEDWARDMIR59915931~ NM…
🌐
CodexWorld
codexworld.com › home › how to guides › how to remove special characters from string in php
How to Remove Special Characters from String in PHP - CodexWorld
September 13, 2018 - The following code removes the special characters from string with regular expression pattern (Regex) in PHP. $string = "Wel%come *to( codex<world, the |world o^f pro?gramm&ing."; // Remove special characters $cleanStr = preg_replace('/[^A-Za-z0-9]/', '', $string);
🌐
Digital Point
forums.digitalpoint.com › threads › preg_replace-to-allow-special-characters.1530917
preg_replace to allow special characters
Hello I am using following code to allow characters like A to Z and numeric values like 0 to 9. $result = preg_replace('/[^a-zA-Z0-9_]/', '',...
Find elsewhere
🌐
Delft Stack
delftstack.com › home › howto › php › remove special characters from string php
How to Remove Special Character in PHP | Delft Stack
February 2, 2024 - On the other hand, if both $pattern and $replaceWith parameters are arrays, then it does the same as in the above condition but replace the specific indexed string of $pattern with its responding counter index of $replaceWith. $string: This is the desirable string you want to filter with a special character. See the example below. <?php function RemoveSpecialChar($str) { $res = preg_replace('/[0-9\@\.\;\" "]+/', '', $str); return $res; } $str = "My name is hello and email hello.world598@gmail.com;"; $str1 = RemoveSpecialChar($str); echo "My UpdatedString: ", $str1; ?>
🌐
CodeSpeedy
codespeedy.com › home › remove special characters from string in php
Remove special characters from string in PHP - CodeSpeedy
September 15, 2022 - <!DOCTYPE html> <html> <body> <?php $str = "code@5 speedy!3*^&% tutorial#$"; $str = str_replace(' ','-',$str); $str = preg_replace('/[^A-Za-z\-]/', '', $str); // Printing the result echo $str; ?> </body> </html>
🌐
sebhastian
sebhastian.com › php-remove-special-characters
Remove special characters from a PHP string | sebhastian
November 28, 2022 - If you need to remove only specific special characters, it’s better to use the str_replace() function. When you need to remove all special characters, use the preg_replace() function.
🌐
W3Resource
w3resource.com › php-exercises › php-regular-expression-exercise-7.php
PHP Regular Expression Exercise: Remove all characters from a string except a-z A-Z 0-9 or blank - w3resource
March 31, 2026 - <?php // Define the input string $string = 'abcde$ddfd @abcd )der]'; // Print the old string echo 'Old string : '.$string.''; // Remove all characters except letters, numbers, and spaces using regular expression $newstr = preg_replace("/[^A-Za-z0-9 ]/", '', $string); // Print the new string after removing unwanted characters echo 'New string : '.$newstr."\n"; ?>