I've never used regular expressions before and did some research on how to allow my username field only alphanumeric characters, dashes, dots, and underscores. I have the following expression but it doesn't seem to be working.
$string = "Joe_Scotto";
if (!preg_match('[a-zA-Z0-9_-.]', $string)) {
echo "Does not match Regex";
} else {
echo "Matches";
}
I want the statement to return true if it is following the "guidelines" and false if the username contains something other than what I specified it should contain. Any help would be great. Thanks!
Try this
$string = "Joe_Scotto";
if (!preg_match('/^[A-Za-z0-9_.]+$/', $string)) {
echo "Does not match Regex";
} else {
echo "Matches";
}
You match only a single character. Try this:
$string = "Joe_Scotto";
if (!preg_match('/^[a-zA-Z0-9_.-]+$/', $string)) {
echo "Does not match Regex";
} else {
echo "Matches";
}
The + sign says: match 1 or more characters defined directly before the + (* is the same but matches 0 or more characters).
Also the separators '/' (or any other separator characters) are required.
And in character classes, it is better to place the - sign to the end, else it could be misinterpreted as range from _ to .
And add ^ at the beginning (this means: match from the beginning of the input) and $ to the end (this means: match to the end of the input). Else, also a part of the string would match.
You should use something like that http://www.phpliveregex.com/p/ern
$string = 'John_Buss';
if (preg_match('/[A-z0-9_\-.]+/', $string)) {
return true;
} else {
return false;
}
Make sure to add / delimiter character at the start and the end of your regex
Make sure to use \ escape character before -
Make sure to add + character quantifier
Related
I try to find a preg_match that filters strings that contain only special characters like -.
However, the string itself can contain special characters but it should not be special characters only.
Any ideas how to do that?
Result should return true if string only contains special characters. So something like
if(preg_match('//',$string)) echo $string; //I leave the pattern empty as this is the actual question.
You need to match the string for any alphanumeric character.
$one= "%^&";
$two = "asd%asd";
function notOnlySpecialChars($str) {
if (preg_match('/[A-Za-z0-9]/', $str)) { //Replaced _ with -
return true;
} else {
return false;
}
}
notOnlySpecialChars($one); //false
notOnlySpecialChars($two); //true
How to check matching value using preg_match in PHP. Could you please check below samples and help me write a regular expression?
Sample values are
1: sam.s_655
2: sara.t_993
3: suyathi.s_633
4: siraj.t_912
<?php
$val = 'sara.t_993';
if (preg_match('', $val)) {
print "Got match!\n";
}
?>
Try this.
$val = "sara.t_993";
if (preg_match('#^[a-z]+\.[a-z]{1}_[0-9]{3}$#', $val)) {
print "Got match!\n";
}
[a-z]+ = one or more a-z
\. = one dot, dot must be escaped, is a special char in regex
[a-z]{1} = one a-z
_ = one undercore
[0-9]{3} = three numbers
^ ... $ = for full match , so siraj.t_912abc wont match
I think this is the match you are looking for:
preg_match('/^\w*\.\w\_\d{3}$/', $look);
I'm using preg_match as a way to validate inputs on a form. Specifically, I am trying to validate input of currency. Here is the function:
if (preg_match("/^\$(((\d{1,3},)+\d{3})|\d+)\.\d{2}$/i", $inString)) {
return True;
} else {
return False;
}
I can get this to work AT ALL. I keeps returning False regardless of what I feed it (including valid strings). I'm sure I'm doing something obviously wrong, but I can't see it. You know how it is...
Anyone have any suggestions?
Try something like this:
$inString = '1550.50';
if (preg_match("/\b\d{1,3}(?:,?\d{3})*(?:\.\d{2})?\b/", $inString)) {
echo "True";
} else {
echo "False";
}
explanation:
\b # word boundary assertion
\d{1,3} # 1-3 digits
(?: # followed by this group...
,? # an optional comma
\d{3} # exactly three digits
)* # ...any number of times
(?: # followed by this group...
\. # a literal dot
\d{2} # exactly two digits
)? # ...zero or one times
\b # word boundary assertion
The preg_match function already returns True or False depending on whether it matches, so there is no need to return True or False a second time.
This means you can directly echo the values of True or False:
$inString = "$12.50";
$price_regex = '~^\$(((\d{1,3},)+\d{3})|\d+)\.\d{2}$~';
echo preg_match($price_regex, $inString);
// echoes 1
You can also directly return these values:
return preg_match($price_regex, $inString);
You can perform a Boolean test:
if( preg_match($price_regex, $inString) ) { // take some action }
else { // take another action }
If what you want instead is to echo some value depending on whether there is a match, do this:
echo (preg_match($price_regex, $inString)) ? "**It Matches!**" : "Nah... No match." ;
Notes:
Changed the delimiter to ~ (more legible)
Removed the i flag (there are no letters, so it doesn't need to be case-insensitive)
Both answers given before this work, but here's an explanation of why the original preg_match pattern didn't work.
It's because the pattern is enclosed in double quotes. When PHP sees this, it treats whatever follows a $ sign as a variable name. If you need to include a literal $ sign inside a double quoted string, the dollar has to be preceded by a backslash.
so both of these patterns work:
'/^\$(((\d{1,3},)+\d{3})|\d+)\.\d{2}$/i'
"/^\\$(((\d{1,3},)+\d{3})|\d+)\.\d{2}\$/i"
Obviously, using single quotes is simpler in this case.
In my program basically your only allowed to use words that contain the letters "IOSHZXN"
I'm trying to figure out a way where you can mix up the letters and it will recognize that it matches.
For example, word SHINT does not match since it has a T, but the word SHINX matches because it contains only the a combination of the letters listed (IOSHZXN)
<?php
$word = "IOSHZNX";
$charactersallowed = "IOSHZXN";
if (preg_match('/IOSHZXN/', $word)) {
echo "YES";
} else {
echo "NO";
}
?>
Any help would be appreciated..
You should use:
if (preg_match('/^[IOSHZXN]+$/', $word)) {
^ and $ make sure the string only the a combination of letters IOSHZXN.
You can do this:
It matches anything that is not one of those letters and returns the opposite:
if (!preg_match('/[^IOSHZXN]+/', $word)) {
echo "YES";
}
Also, if you want it to be case-insensitive, you can use:
if (!preg_match('/[^IOSHZXN]+/i', $word)) {
echo "YES";
}
The [^...] matches anything that is not defined within the brackets.
The + continues to search through the entire string.
The i makes it not care about if letters are capitalized or not.
I have a variable I want to use in a preg_match combined with some regex:
$string = "cheese-123-asdf";
$find = "cheese";
if(preg_match("/$find-/d.*/", $string)) {
echo "matched";
}
In my pattern I am trying to match using cheese, followed by a - and 1 digit, followed by anything else.
change /d to \d
there is no need to use .*
if your string is defined by user (or may contains some characters (e.g: / or * or ...)) this may cause problem on your match.
Code:
<?php
$string = "cheese-123-asdf";
$find = "cheese";
if(preg_match("/$find-\d/", $string))
{
echo "matched";
}
?>
You mistyped / for \:
if(preg_match("/$find-\d.*/", $string)) {
The .* is also not really necessary since the pattern will match either way.
for digit, it's \d
if(preg_match("/$find-\d.*/", $string)) {