$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"
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
$str=preg_replace("/[^0-9a-zA-Z ]/u", "_", $str_test);
Notice 'u' modifier! Explanation: http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php#107498
If the _subject_ contains utf-8 sequences the 'u' modifier should be set, otherwise a pattern such as /./ could match a utf-8 *sequence as two to four individual ASCII characters*.
Why not use the build in php multibyte functions?
mb_ereg_replace is the one to use here. Manual
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.
try to replace the regular expectation change
preg_replace('/[^A-Za-z0-9\-]/', '', $string);
with
preg_replace("/[^A-Za-z0-9\-\']/", '', $string); // escape apostraphe
or
you can str_replace It is quicker and easier than preg_replace() Because it does not use regular expressions.
$text = str_replace("'", '', $string);
In a more detailed manner from Above example, Considering below is your string:
$string = '<div>This..</div> <a>is<a/> <strong>hello</strong> <i>world</i> ! هذا هو مرحبا العالم! !@#$%^&&**(*)<>?:";p[]"/.,\|`~1@#$%^&^&*(()908978867564564534423412313`1`` "Arabic Text نص عربي test 123 و,.m,............ ~~~ ٍ،]ٍْ}~ِ]ٍ}"; ';
Code:
echo preg_replace('/[^A-Za-z0-9 !@#$%^&*().]/u','', strip_tags($string));
Allows: English letters (Capital and small), 0 to 9 and characters !@#$%^&*().
Removes: All html tags, and special characters other than above
You can use:
$str = preg_replace('/^=+\h*\K.+?(?=\h*=)/m', 'DONE', $str);
RegEx Demo
RegEx Breakup:
^ # Line start
=+ # Match 1 or more =
\h* # Match or more horizontal spaces
\K # resets the starting point of the reported match
.+? # match 1 or more of any character (non-greedy)
(?=\h*=) # Lookahead to make sure 0 or more space followed by 1 = is there
You have to place the =s back.
Also, instead of .* use [^=]* (matches characters, which are not =) so that the =s don't get eaten up for the replacement.
Additionally, you don't have to escape =:
preg_replace("/(=+)([^=]*)(=+)/","$1 DONE $3", $paragraph);
See it in action
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);