How can I check whether is a string contains only English letters? - php

I use PHP and I need to check whether is a string made of just
English Lowercase letter
dash
underline?
Something like this:
if ( /* the condition */ ) {
// Yes, all characters of the string are English lowercase letters or dash or underline
} else {
// No, there is at least one unexpected character
}
Here is some examples:
$str = "test"; // true
$str = "test_-'; // true
$str = "t-s"; // true
$str = "test1"; // false
$str = "Test"; // false
$str = "test?"; // false

To match a whole string that only consists of 1 or more lowercase ASCII letters, hyphen or underscores, use
/^[-a-z_]+$/D
See the regex demo
Details:
^ - start of string
[-a-z_]+ - 1 or more ASCII lowercase letters, hyphens or underscores
$ - end of string
/D - the modifier that will make $ match the very end of the string (otherwise, $ will also match a newline that appears at the end of the string).
PHP:
if (preg_match('/^[-a-z_]+$/D', $input)) {
// Yes, all characters of the string are English lowercase letters or dash or underline
} else {
// No, there is at least one unexpected character
}

Use the PHP function
preg_match()
with this regular expression:
$regex = [a-z\_\-]+
The \ are to escape out the underscore and dash. + means you have to have at least 1 character.
This is a handy tool for regular expressions http://www.regexpal.com/

Try this on for size
/**
* Test if a string matches our criteria
*/
function stringTestOk($str) {
return !(preg_match_all('/[^a-z_\-]/', $str) > 0);
}
// Examples
foreach(['test', 'test_-', 't-s', 'test1', 'Test', 'test?'] as $str) {
echo $str, ' ', (stringTestOk($str) ? 'true' : 'false'), PHP_EOL;
}

Related

preg_match replace ALL condition with ANY

Seems like
preg_match('/^[\p{Cyrillic}]+$/', $str)
returns 0 or 1 based on if $str contains ALL Cyrillic letters.
I need 0 or 1 based on if $str contains ANY Cyrillic letters.
Thank you.
You can use:
$ret = preg_match('/\p{Cyrillic}/u', $str);
to figure if input string contains any Cyrillic character or not. /u flag is required to handle unicode string inputs.
Alternatively use mb_ereg function for multibyte regex match like this:
$str = 'БДКЯ'; // string with Cyrillic characters only
// check with Cyrillic string only
var_dump( mb_ereg('\p{Cyrillic}', $str) ); // int(1)
// check with mix of Cyrillic and ASCII characters
var_dump( mb_ereg('\p{Cyrillic}', $str . 'abc') ); // int(1)
// check with ASCII characters only
var_dump( mb_ereg('\p{Cyrillic}', 'abc') ); // bool(false)
The anchors ^ and $ force the {Cyrillic} character match from the beginning to the end of the string, so remove them. Also, the character class [] and + are not needed because you are looking for any match:
/\p{Cyrillic}/

PHP preg_match - only allow alphanumeric strings and - _ characters

I need the regex to check if a string only contains numbers, letters, hyphens or underscore
$string1 = "This is a string*";
$string2 = "this_is-a-string";
if(preg_match('******', $string1){
echo "String 1 not acceptable acceptable";
// String2 acceptable
}
Code:
if(preg_match('/[^a-z_\-0-9]/i', $string))
{
echo "not valid string";
}
Explanation:
[] => character class definition
^ => negate the class
a-z => chars from 'a' to 'z'
_ => underscore
- => hyphen '-' (You need to escape it)
0-9 => numbers (from zero to nine)
The 'i' modifier at the end of the regex is for 'case-insensitive' if you don't put that you will need to add the upper case characters in the code before by doing A-Z
if(!preg_match('/^[\w-]+$/', $string1)) {
echo "String 1 not acceptable acceptable";
// String2 acceptable
}
Here is one equivalent of the accepted answer for the UTF-8 world.
if (!preg_match('/^[\p{L}\p{N}_-]+$/u', $string)){
//Disallowed Character In $string
}
Explanation:
[] => character class definition
p{L} => matches any kind of letter character from any language
p{N} => matches any kind of numeric character
_- => matches underscore and hyphen
+ => Quantifier — Matches between one to unlimited times (greedy)
/u => Unicode modifier. Pattern strings are treated as UTF-16. Also
causes escape sequences to match unicode characters
Note, that if the hyphen is the last character in the class definition it does not need to be escaped. If the dash appears elsewhere in the class definition it needs to be escaped, as it will be seen as a range character rather then a hyphen.
\w\- is probably the best but here just another alternative
Use [:alnum:]
if(!preg_match("/[^[:alnum:]\-_]/",$str)) echo "valid";
demo1 | demo2
Why to use regex? PHP has some built in functionality to do that
<?php
$valid_symbols = array('-', '_');
$string1 = "This is a string*";
$string2 = "this_is-a-string";
if(preg_match('/\s/',$string1) || !ctype_alnum(str_replace($valid_symbols, '', $string1))) {
echo "String 1 not acceptable acceptable";
}
?>
preg_match('/\s/',$username) will check for blank space
!ctype_alnum(str_replace($valid_symbols, '', $string1)) will check for valid_symbols

regex how would i search for asterisk-space-equals sign?

what regex would be used to search for an asterisk followed by a space followed by an equlas sign?
that is '* =' ?
Using preg_match in php would find a match in these strings:
'yet again * = it happens'
'again* =it happens'
and what is simplest way to search for an exact word for word, number for number, puncuation sign for puncuation sign string in regex?
You don't need a regular expression here. Consider using strpos
$pos = strpos('yet again * = it happens', '* =');
if(pos === false){
// not there
}
else {
// found
}
If you must use preg_match, remember the delimiters:
preg_match('/\* =/', $str, $matches);
In this case you have to escape the asterisk. You may want to allow more spaces, for example with the pattern \*\h+=. \h stands for horizontal whitespace characters.
Try this regex pattern
\*(?=\s=)
<?php
function runme($txt) {
return preg_match("/^\*\ \=$/", $txt);
}
echo runme($argv[1])? "$argv[1] matches!\n" : "$argv[1] is not asterisk space equals-sign\n";
?>
$ php asterisk_space_equals.php 'yet again * = it happens'
yet again * = it happens is not asterisk space equals-sign
$ php asterisk_space_equals.php 'again* =it happens'
again* =it happens is not asterisk space equals-sign
$ php asterisk_space_equals.php '* ='
* = matches!

php regex for checking a string

I need a regular expression for checking a string, that it has more than 2 symbols length, first symbol should be Alphabetic, last symbol should be '_'.
And how can I uppercase only first symbol?
Thank you.
To match
preg_match( "/^[a-z].+_$/i", $str );
To uppercase the first letter
ucfirst( $str );
To match an input with at least 3 char with first being uppercase alphabet and last begin underscore use:
^[A-Z].+_$
To allow any alphabet in the beginning you can use:
^[A-Za-z].+_$
Try this :
/^[a-zA-Z].+_$/
Note:
instead of .+ you could use [some allowed chars]+
You could also do it like this (might save further regular expression dithering down the line):
if ((strlen($str) < 3) || !ctype_alpha($str[0]) || (strrchr($str, '_') != '_')) {
echo 'Invalid string';
} else {
$str = ucfirst($str);
}

Allow only [a-z][A-Z][0-9] in string using PHP

How can I get a string that only contains a to z, A to Z, 0 to 9 and some symbols?
You can filter it like:
$text = preg_replace("/[^a-zA-Z0-9]+/", "", $text);
As for some symbols, you should be more specific
You can test your string (let $str) using preg_match:
if(preg_match("/^[a-zA-Z0-9]+$/", $str) == 1) {
// string only contain the a to z , A to Z, 0 to 9
}
If you need more symbols you can add them before ]
Don't need regex, you can use the Ctype functions:
ctype_alnum: Check for alphanumeric character(s)
ctype_alpha: Check for alphabetic character(s)
ctype_cntrl: Check for control character(s)
ctype_digit: Check for numeric character(s)
ctype_graph: Check for any printable character(s) except space
ctype_lower: Check for lowercase character(s)
ctype_print: Check for printable character(s)
ctype_punct: Check for any printable character which is not whitespace or an alphanumeric character
ctype_space: Check for whitespace character(s)
ctype_upper: Check for uppercase character(s)
ctype_xdigit: Check for character(s) representing a hexadecimal digit
In your case use ctype_alnum, example:
if (ctype_alnum($str)) {
//...
}
Example:
<?php
$strings = array('AbCd1zyZ9', 'foo!#$bar');
foreach ($strings as $testcase) {
if (ctype_alnum($testcase)) {
echo 'The string ', $testcase, ' consists of all letters or digits.';
} else {
echo 'The string ', $testcase, ' don\'t consists of all letters or digits.';
}
}
Online example: https://ideone.com/BYN2Gn
Both these regexes should do it:
$str = preg_replace('~[^a-z0-9]+~i', '', $str);
Or:
$str = preg_replace('~[^a-zA-Z0-9]+~', '', $str);
A shortcut will be as below also:
if (preg_match('/^[\w\.]+$/', $str)) {
echo 'Str is valid and allowed';
} else
echo 'Str is invalid';
Here:
// string only contain the a to z , A to Z, 0 to 9 and _ (underscore)
\w - matches [a-zA-Z0-9_]+
Hope it helps!
If you need to preserve spaces in your string do this
$text = preg_replace("/[^a-zA-Z0-9 ]+/", "", $text);
Please note the way I have added space between 9 and the closing bracket. For example
$name = "!#$John Doe";
echo preg_replace("/[^a-zA-Z0-9 ]+/", "", $name);
the output will be:
John Doe
Spaces in the string will be preserved.
If you fail to include the space between 9 and the closing bracket the output will be:
JohnDoe
Hope it helps someone.
The best and most flexible way to accomplish that is using regular expressions.
But I`m not sure how to do that in PHP but this article can help. link

Categories