I'm looking for a php preg replace to null/empty string if string contains any non alphanumeric characters or spaces
e.g. Strings
$string = "This string is ok";
$string = "Thi$ string is NOT ok, and should be emptied"
When I say emptied/nulled I mean it will make the string "".
So basically anything a-z A-Z 0-9 or space is ok
Any ideas?
if(preg_match('~[^a-z0-9 ]~i', $str))
$str = '';
You can use this pattern (note the possessive quantifier) to match "invalid" strings:
^[a-zA-Z0-9 ]*+.+$
Here's a snippet:
<?php
$test = array(
"This string is ok",
"Thi$ string is NOT ok, and should be emptied",
"No way!!!",
"YES YES YES"
);
foreach ($test as $str) {
echo preg_replace('/^[a-zA-Z0-9 ]*+.+$/', '<censored!>', $str)."\n";
}
?>
The above prints (as seen on ideone.com):
This string is ok
<censored!>
<censored!>
YES YES YES
It works by using possessive repetition (i.e. no backtracking) to match as many valid characters as possible with [a-zA-Z0-9 ]*+. If there's anything left after this, i.e. .+ matches, then we must have gotten stuck at an invalid character, so the whole string gets matched (and thus replaced). Otherwise the string remains untouched.
The string '<censored!>' is used as replacement here for clarity; you can use the empty string '' if that's what you need.
References
regular-expressions.info/Possessive Quantifier
Related
I have this regular expression /^(string#)([^#\s\\\\]|(\\\d{3}))*$/.
It's supposed to accept strings that start with string#, then all characters but #, \, or any whitespace characters can follow. \ can only appear when it's followed by 3 digits.
When I test this regex with the string string#test\07test on regex101.com and phpliveregex.com, no matches are found (correctly), but preg_match() returns true for the same string.
What am I doing wrong?
Thank you
UPDATE:
$regex = "/^(string#)([^#\s\\\\]|(\\\d{3}))*$/";
preg_match($regex, 'string#test\07test'); # this returns false, as it should
preg_match($regex, "string#test\07test"); # this returns true
The test string is taken from a file using fgets() and stored in a variable, though, so I can't pass it to preg_match() and put apostrophes around it, I need to pass the variable ($string, for example), and preg_match($regex, $string) incorrectly returns true again.
First things first:
'\07' is a string of length 3 beginning with a backslash character.
"\07" is a string of length 1 consisting of ASCII x'07'
Interactive shell
php > echo strlen('\07');
3
php > echo strlen("\07");
1
php >
See PHP Strings
Regex:
/^string#(?:[^#\s\\]|\\(?=\d{3}))*$/
See Regex Demo
^ Matches start of string.
string# Matches string#.
[^#\s\\] Matches any character other than #, white space or \.
| or
\\(?=\d{3}) Matches \ if followed by 3 digits.
(?: expression 3 through 5)* matched 0 or more times.
$ Matches the end of string.
Code:
<?php
$tests = [
'abcd',
'string# test',
'string##test',
'string#\07test',
'string#\075test',
'string#test'
];
$regex = '/^string#(?:[^#\s\\\\]|\\\\(?=\d{3}))*$/';
foreach ($tests as $test) {
if (preg_match($regex, $test)) {
echo "$test\n";
}
}
Prints:
string#\075test
string#test
I Have one string like below.
$string = "2346#$ABSC$%###234567";
Now I want last character from this string that is not numeric or special character, It should be only A-a to Z-z.
Means, I need only "C" from this string.
I have try this formula:
substr($string, -1);
You should look into regular expressions using something like preg_match()
An expression like this would match:
/([a-z])[^a-z]*$/i
It means:
([a-z]) Capture an a-z character (the i at the end makes it case-insensitive)
[^a-z]*$ followed by 0 or more non a-z characters until the end of the string
See an example.
This should work for you:
(Here I just replace everything expect a-zA-Z with an empty string. After this I just access the last character)
<?php
$string = '2346#$ABSC$%###234567';
$string = preg_replace("/[^a-zA-Z]/", "", $string);
echo $string[strlen($string)-1];
?>
output:
C
The proper regex is: ([a-z])[^a-z]*$
I have written the following code to check if the given string is Latin or it contains some other non-latin chars like Persian. The issue is that it always returns true for both of the following strings:
$str = "Hello, What's up?"
Or
$str = "Hello, سلام"
While for the second string it should return false since it contains Persian characters (non-latin) too.
$default_rule = "/[a-zA-Z0-9\(\)\*_\-\!\#\$\%\^\&\*\,\.\"\'\]\[]*/";
$rule = ($rule==null) ? $default_rule : $rule;
if(preg_match($rule, $str)==true)
{
// always returns true
}
Your pattern will return true if the string contains zero or more of those characters you've specified. In other words, it will return true for any string at all. You need to put start (^) and end ($) anchors around it. Also you don't need to escape most of those characters (the character class causes them to be treated as literal characters):
$default_rule = '/^[a-zA-Z0-9()*_\-!#$%^&*,."\'\][]*$/';
But, this will match an empty string. To also make sure that the string is not empty, use the + quantifier (one or more) instead of *.
$default_rule = '/^[a-zA-Z0-9()*_\-!#$%^&*,."\'\][]+$/';
Consider example:
$mystring = "us100ch121jp23uk12";
I) I want to change value of jp by adding +1 so that makes the string into
us100ch121jp24uk12
suppose if
II) Is there a way to seperate the numeric part and alphabetic part in the above string into:
[us , 100]
[ch,121]
[jp,24]
[us,12]
my code:
$string = "us100ch121jp23uk12";
$search_for = "us";
$pairs = explode("[]", $string); // I dont know the parameters.
foreach ($pairs as $index=>$pair)
{
$numbers = explode(',',$pair);
if ($numbers[0] == $search_for){
$numbers[1] += 1; // 23 + 1 = 24
$pairs[index] = implode(',',$numbers); //push them back
break;
}
}
$new_string = implode('|',$pairs);
using Evan sir's suggestions
$mystring = "us100ch121jp22uk12";
preg_match_all("/([A-z]+)(\d+)/", $mystring, $output);
//echo $output[0][4];
foreach($output[0] as $key=>$value) {
// echo "[".$value."]";
echo "[".substr($value, 0, 2).",".substr($value, 2, strlen($value) - 2)."]"."<br>";
}
If you use preg_match_all("/([A-z]+)(\d+)/", $string, $output);, it will return an array to $output that contains three arrays. The first array will be country number strings (eg 'us100'). The second will contain country strings (eg 'us'). The third will contain the numbers (eg '100').
Since the second and third arrays will have matching indexes ($output[1][0] will be 'us' and $output[2][0] will be '100'), you could just cycle through those and do whatever you'd like to them.
Here is more information about using regular expressions in PHP. The site also contains information about regular expressions in general, which are a useful tool for any programmer!
You can do it using regular expressions in PHP. See tutorial:
http://w3school.in/w3schools-php-tutorial/php-regular-expression/
Function Description
ereg_replace() The ereg_replace() function finds for string specified by pattern and replaces pattern with replacement if found.
eregi_replace() The eregi_replace() function works similar to ereg_replace(), except that the search for pattern in string is not case sensitive.
preg_replace() The preg_replace() function works similar to ereg_replace(), except that regular expressions can be used in the pattern and replacement input parameters.
preg_match() The preg_match() function finds string of a pattern and returns true if pattern matches false otherwise.
Expression Description
[0-9] It matches any decimal digit from 0 through 9.
[a-z] It matches any character from lowercase a through lowercase z.
[A-Z] It matches any character from uppercase A through uppercase Z.
[a-Z] It matches any character from lowercase a through uppercase Z.
p+ It matches any string containing at least one p.
p* It matches any string containing zero or more p’s.
p? It matches any string containing zero or more p’s. This is just an alternative way to use p*.
p{N} It matches any string containing a sequence of N p’s
p{2,3} It matches any string containing a sequence of two or three p’s.
p{2, } It matches any string containing a sequence of at least two p’s.
p$ It matches any string with p at the end of it.
^p It matches any string with p at the beginning of it.
[^a-zA-Z] It matches any string not containing any of the characters ranging from a through z and A through Z.
p.p It matches any string containing p, followed by any character, in turn followed by another p.
^.{2}$ It matches any string containing exactly two characters.
<b>(.*)</b> It matches any string enclosed within <b> and </b>.
p(hp)* It matches any string containing a p followed by zero or more instances of the sequence hp.
you also can use JavaScript:
http://www.w3schools.com/jsref/jsref_obj_regexp.asp
I am trying to verify in PHP with preg_match that an input string contains only "a-z, A-Z, -, _ ,0-9" characters. If it contains just these, then validate.
I tried to search on google but I could not find anything usefull.
Can anybody help?
Thank you !
Use the pattern '/^[A-Za-z0-9_-]*$/', if an empty string is also valid. Otherwise '/^[A-Za-z0-9_-]+$/'
So:
$yourString = "blahblah";
if (preg_match('/^[A-Za-z0-9_-]*$/', $yourString)) {
#your string is good
}
Also, note that you want to put a '-' last in the character class as part of the character class, that way it is read as a literal '-' and not the dash between two characters such as the hyphen between A-Z.
$data = 'abc123-_';
echo preg_match('/^[\w|\-]+$/', $data); //match and output 1
$data = 'abc..';
echo preg_match('/^[\w|\-]+$/', $data); //not match and output 0
You can use preg_replace($pattern, $replacement, $subject):
if (preg_replace('/[A-Za-z0-9\-\_]/', '', $string)) {
echo "Detect non valid character inside the string";
}
The idea is to remove any valid chars, if the result is NOT empty do the code.