$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.
$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.
If you by writing "non letters and numbers" exclude more than [A-Za-z0-9] (ie. considering letters like åäö to be letters to) and want to be able to accurately handle UTF-8 strings \p{L} and \p{N} will be of aid.
\p{N}will match any "Number"\p{L}will match any "Letter Character", which includes- Lower case letter
- Modifier letter
- Other letter
- Title case letter
- Upper case letter
Documentation PHP: Unicode Character Properties
$data = "Thäre!wouldn't%bé#äny";
$new_data = str_replace ("'", "", $data);
$new_data = preg_replace ('/[^\p{L}\p{N}]/u', '_', $new_data);
var_dump (
$new_data
);
output
string(23) "Thäre_wouldnt_bé_äny"
preg_replace remove special characters - PHP Coding Help - PHP Freaks
How can I preg_replace with special characters, numbers and everything? - PHP - SitePoint Forums | Web Development & Design Community
preg_replace to allow special characters
php - Preg_replace special characters & - Stack Overflow
You do not need to escape & only the ?
$reg = preg_replace("#&&#", "&", $reg);
$reg = preg_replace("#\?&#", "?", $reg);
You can simplify the two regexs into one.
echo preg_replace("#([?&])\s*&#", "$1", ' ? &lang=en');
Output:
?lang=en
Your modifiers didn't make sense since you aren't using alpha characters or the ..
Also & isn't a special regex character, just ?. If in a character class ([]) neither will need to be replaced.
Regex101 Demo: https://regex101.com/r/iS4mQ0/1
I think you want something like this pattern?
(\?dead(=[^&]*|))*
PHP Code:
echo preg_replace('/(\?dead(=[^&]*|))*/','',$sourcestring);
This will produce this output of your given urls:
http://www.url.com/&wow=test
http://www.url.com/&wow=test
http://www.url.com/?hello
You can use \Q and \E (as in QuotE) when dealing with lots of special characters.
The text between these delimiters will be treated literally.
That regex just seems to have a lot of unnecessary problems. As Otala said, the hypen isn't escaped, the pipe isn't escaped and it's not looking for periods. Also it had two checks for hyphens.
You should simplify it by simply looking for all non-pipe characters up to the pipe:
preg_replace("/([^|\r\n]+)\|/", '<h3 id="${1}">${1}</h3>', $content);
Could simplify your regex a bit:
preg_replace("/([^|\n\r]+\|\n)/", '${1}', $content);
You need to add /u pattern modifier to your pattern to turn on UTF-8 support in PCRE. This is assuming everything is in UTF-8 already.
http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php
$str = '$#$<<>-//La souris a été mangée par le chat ';
$str = preg_replace('/[^a-zA-Z0-9éàêïòé\,\.\']/u',' ',trim($str));
$str = '$#$<<>-//La souris a été mangée par le chat ';
$str = preg_replace('/[^\p{L}\,\.\']/u',' ',trim($str));
Both the snippets worked for me, on PHP 5.3. The second regular expression is less restricted, and accepts all Unicode letters.