$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
There is actually no need to even loop the array.
Str_replace accepts arrays.
Add the %% at start and end of each key in the array and this code will replace them all.
$vars = array(
'%%name%%' => $name,
'%%email%%' => $email,
'%%your_name%%' => $directorname,
'%%logininfo%%' => $loginInfo,
'%%directorname%%' => $directorname,
'%%campname%%' => $campname,
'%%numvideos%%' => NUM_VIDEOS);
$message = str_replace(array_keys($vars) , $vars , $message);
See here for a working example https://3v4l.org/T8YGs
If you still want to use preg_replace you need to escape the
$ in the string.See this regex101 for an example, https://regex101.com/r/BRJDCV/1
For something this basic you're better off using str_replace: http://php.net/str_replace
Example:
/* Replace %%VARIABLE%% using vars*/
foreach($vars as $key => $value)
{
$message = str_replace('%%' . $key . '%%', $value, $message);
}
This should prevent any issues with special characters in the password.
Please note, your question doesn't specify but if you're sending this message in an email you should NOT send a password in an email. Email is not a secure medium.
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.