I have the following PHP code to remove special characters from a variable;
<?php
$name = "my%^$##name8";
$patterns = array( '/\s+/' => '_', '/&/' => 'and', '/[^[:alpha:]]+/' => '_');
$name2 = preg_replace(array_keys($patterns), array_values($patterns), trim($name));
echo $name2;
?>
But, along with special chars, numbers also are getting replaced with underscores_. I want to include numbers in the result. How can I fix this?
Your third pattern, /[^[:alpha:]]+/ is replacing everything that's not a letter with an underscore. So add numbers to it, like /[^[:alpha:]0-9]+/
Replace '/[^[:alpha:]]+/' with '/[^[:alpha:][:digit:]]+/'. The original is replacing anything that's not an alphabetic character. Adding [:digit:] means it will replace anything that's not a letter or a number, so your numbers will be preserved as well.
Related
Consider the following example....
The issue is with the preg_replace which is used to replace the variables total_balance_overriden and total_balance.
$text = 'this: {{total_balance_overridden}} - that: {{total_balance}}';
$total = '$517.50';
$overiddentotal = '$390.00';
$text = preg_replace('/{{total_balance_overridden}}/', $overiddentotal, $text);
$text = preg_replace('/{{total_balance}}/', $total, $text);
echo $total;
echo $overiddentotal;
echo $text;
This gives me...
$517.50
$390.00
this: 0.00 - that: 7.50
It appears that the $total and $overiddentotal vars have the correct output, but when they have been replaced using the preg_replace, their length has been stripped, and the currency sign and first two numbers are missing. Any ideas why?
Note: If i replace the dollar sign with a pound sign it works! I get...
this: £390.00 - that: £517.50
So is the dollar sign and 2 numbers some sort of special character or var thats getting stripped?
Dollar signs are special characters in replacement strings, they normally refer to captured substrings from the match. If you want a literal dollar sign, you have to escape it:
$total = '\$517.50';
$overiddentotal = '\$390.00';
Note also, in this case, there's no need for regex at all. Just use str_replace() and you won't have this issue.
I'm working with text content in UTF8 encoding stored in variable $title.
Using preg_replace, how do I append an extra space if the $title string is ending with:
upper/lower case character
digit
symbol, eg. ? or !
This should do the trick:
preg_replace('/^(.*[\w?!])$/', "$1 ", $string);
In essence what it does is if the string ends in one of your unwanted characters it appends a single space.
If the string doesn't match the pattern, then preg_replace() returns the original string - so you're still good.
If you need to expand your list of unwanted endings you can just add them into the character block [\w?!]
Using a positive lookbehind before the end of the line.
And replace with a space.
$title = preg_replace('/(?<=[A-Za-z0-9?!])$/',' ', $title);
Try it here
You may want to try this Pattern Matching below to see if that does it for you.
<?php
// THE REGEX BELOW MATCHES THE ENDING LOWER & UPPER-CASED CHARACTERS, DIGITS
// AND SYMBOLS LIKE "?" AND "!" AND EVEN A DOT "."
// HOWEVER YOU CAN IMPROVISE ON YOUR OWN
$rxPattern = "#([\!\?a-zA-Z0-9\.])$#";
$title = "What is your name?";
var_dump($title);
// AND HERE, YOU APPEND A SINGLE SPACE AFTER THE MATCHED STRING
$title = preg_replace($rxPattern, "$1 ", $title);
var_dump($title);
// THE FIRST var_dump($title) PRODUCES:
// 'What is your name?' (length=18)
// AND THE SECOND var_dump($title) PRODUCES
// 'What is your name? ' (length=19) <== NOTICE THE LENGTH FROM ADDED SPACE.
You may test it out HERE.
Cheers...
You need
$title=preg_replace("/.*[\w?!]$/", "\\0 ", $title);
I've got a string called $ID coming in from a different page and hitting base64_decode($enc); and want to check it for any weird characters. $ID when decrypted should only contain letters, numbers, underscores and dashes.
I've had a bit of a look at preg_replace('/[\x80-\xFF]/', '', $string); which cuts out some weird characters---which is helpful---but I can still see sometimes that # signs and brackets and stuff still make it in.
Is there a way I can lower the ascii test? Or how else do I cut out everything except letters, numbers, underscores and dashes?
Any help at pointing me in the right direction is wonderful and thanks!
$enc = $_GET["key"];
$ID= base64_decode($enc);
if (empty($enc)) { echo "key is empty"; } else {
echo "string ok<br>";
$check = preg_replace('/[\x80-\xFF]/', '', $ID);
echo $check;
// i can see this step is helping cut junk out, do more tests from here
}
Typing a caret after the opening square bracket negates the character class, so you can do:
$check = preg_replace('/[^A-Za-z0-9_-]/', '', $ID);
You can use this replacement:
$check = preg_replace('~[^[:word:]-]+~', '', $ID);
The [:word:] character class contains letters, digits and the underscore.
To make the string lowercase, use strtolower()
I want to remove all non-alphanumeric signs from left and right of the string, leaving the ones in middle of string.
I've asked similar question here, and good solution is:
$str = preg_replace('/^\W*(.*\w)\W*$/', '$1', $str);
But it does remove also some signs like ąĄćĆęĘ etc and it should not as its still alphabetical sign.
Above example would do:
~~AAA~~ => AAA (OK)
~~AA*AA~~ => AA*AA (OK)
~~ŚAAÓ~~ => AA (BAD)
Make sure you use u flag for unicode while using your regex.
Following works with your input:
$str = preg_replace('/^\W*(.*\w)\W*$/u', '$1', '~~ŚAAÓ~~' );
// str = ŚAAÓ
But this won't work: (Don't Use it)
$str = preg_replace('/^\W*(.*\w)\W*$/', '$1', '~~ŚAAÓ~~' );
You can pass in a list of valid characters and tell the function to replace any character that is not in that list:
$str = preg_replace('/[^a-zA-Z0-9*]+/', '', $str);
The square brackets say select everything in this range. The carat (^) is the regex for not. We then list our valid characters (lower case a to z, uppercase a to z, numbers from 0 to 9, and an asterisks). The plus symbol on the end of the square bracket says select 0 or more characters.
Edit:
If this is the list of all characters you want to keep, then:
$str = preg_replace('/[^ĄąĆ毿ŹźŃńŁłÓó*]+/', '', $str);
I'm making a form that asks for the user's first and last name, and I don't want them entering
$heil4
I would like them to enter
Sheila
I know how to filter out everything except letters, but I'm aware that some names can have
Sheila-McDonald
So how would I remove everything from a string apart from letters and a hyphen?
Simply use
$s = preg_replace("/[^a-z-]/i", "", $s);
or if you want to convert some non-ascii characters to ascii, such as Jean-Rémy to Jean-Remy, then use
$s = preg_replace("/[^a-z-]/i", "", iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $s));
Instead of replacing with nothing, have some fun. that way a name that consists mainly of numbers you can decode ;p
$name = '$h3il4-McD0nald';
$find = array(0,1,3,4,5,6,7,'$');
$replace = array('o','l','e','a','s','g','t','s');
$name = str_replace($find,$replace,$name);
//Sheila-McDonald
echo ucfirst(preg_replace('/[^a-z-]/i', '', $name));
$new = preg_replace('#[^A-Z-]#iu', '', $data);
but instead of removing letters (and thus modifying user's input) better validate it
and show an error if the input is not valid. This way the user will know that what he had entered is exactly the value you have
if(!preg_match('#[A-Z-]#iu', $data)) echo 'invalid';
Use this to strip out all non alpha-numeric characters, not including non latin characters, and prescribed punctuation.
$strtochange= preg_replace("/[^\s\p{Pd}a-zA-ZÀ-ÿ]/",'',$strtochange);
Note: this will turn $heil4 into heil.