$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
🌐
Regular-Expressions.info
regular-expressions.info › replacecharacters.html
Replacement Text Tutorial - Special Characters
You can’t and needn’t escape ... is an error in Python but a literal backslash in Ruby. In PHP’s preg_replace, you can use a backslash to escape the backslash and the dollar....
🌐
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
Find elsewhere
🌐
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_]/', '',...
🌐
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 formatting / syntax of regexes, but it is way too far over my comprehension abilities as a new coder. I want to create a code that will do these these things: Remove most special char…
🌐
W3Schools
w3schools.com › php › func_regex_preg_replace.asp
PHP preg_replace() Function
The preg_replace() function returns a string or array of strings where all matches of a pattern or list of patterns found in the input are replaced with substrings. There are three different ways to use this function: 1. One pattern and a ...
🌐
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~ NM1821VENKATARAMANSUBRAMANIANXX1154347748~ REF1L0000FEP000111~’; preg_replace($pattern, $subject, $replacement) echo $subject; How can I do that?