How can I check if a string is at least one letter and one digit in PHP? Can have special characters, but basically needs to have one letter and one digit.
Examples:
$string = 'abcd' //Return false
$string = 'a3bq' //Return true
$string = 'abc#' //Return false
$string = 'a4#e' //Return true
Thanks.
Try this
if (preg_match('/[A-Za-z]/', $string) & preg_match('/\d/', $string) == 1) {
// string contains at least one letter and one number
}
preg_match('/\pL/', $string) && preg_match('/\p{Nd}/', $string)
or
preg_match('/\pL.*\p{Nd}|\p{Nd}.*\pL/', $string)
or
preg_match('/^(?=.*\pL)(?=.*\p{Nd})/', $string)
or
preg_match('/^(?=.*\pL).*\p{Nd}/', $string)
I'm not sure if \d is equivalent to [0-9] or if it matches decimal digits in PHP, so I didn't use it. Use whichever of \d, [0-9] and \p{Nd} that matches that right thing.
The pattern you're looking for is ^.*(?=.*\d)(?=.*[a-zA-Z]).*$
In use:
if( preg_match( "/.*(?=.*\d)(?=.*[a-zA-Z]).*/", $string ) )
echo( "Valid" );
else
echo( "Invalid." );
That should work if latin chars only and numbers:
if (preg_match('/[a-z0-9]+/i', $search_string)) { ... has at least one a-zA-Z0-9 ... }
Related
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;
}
I'm stuck here and I can't find any results for my question, maybe because english is not my native language.
I want to match lines which contain maximum 30 letters/numbers in a sequence:
Is this even possible with preg_match?
preg_match("/[^A-Za-z0-9](max 30 in a sequence)/", $string)
Strings:
$string = "1234567890123456789012345678901234567890"; // FALSE
$string = "sdfihsgbfsadiousdghiug"; // TRUE
$string = "cfgvsdfsdf786sdf78s9d8g7stdg87stdg78tsd7g0tsd9g7t"; // FALSE
$string = "65656.sdfsdf.sdfsdf"; // TRUE
$string = "ewrwet_t876534875634875687te8---7r9w358wt3587tw3587"; // TRUE
$string = "sd879dtg87dftg87dftg87ftg87tfg087tfgtdf8g7tdf87gt8t___454"; // FALSE
You might want to find if there's 30 or more of these characters with:
preg_match("/[A-Za-z0-9]{30,}/", $string)
See matches in bold:
1234567890123456789012345678901234567890
sdfihsgbfsadiousdghiug
cfgvsdfsdf786sdf78s9d8g7stdg87stdg78tsd7g0tsd9g7t
65656.sdfsdf.sdfsdf
ewrwet_t876534875634875687te8---7r9w358wt3587tw3587
sd879dtg87dftg87dftg87ftg87tfg087tfgtdf8g7tdf87gt8t___454
http://regexr.com/3arj2
And then negate the result:
preg_match("/[A-Za-z0-9]{30,}/", $string) === 0
// returns 0 if no match
// or FALSE if error
If you do not want to match alphanumeric strings that are longer than 30 characters, you need to match a non-alphanumeric character at the end and beginning of your expression
preg_match("/[^A-Za-z0-9][A-Za-z0-9]{1,30}[^A-Za-z0-9]/", $string);
Non of your examples will match, your regex is incorrect. You need the ^ outside of the character class, inside it means exclude. If nothing entered is valid this should work. If not change the 0 to a 1.
preg_match("/^[A-Za-z0-9]{0,30}/", $string)
how can I match a the first number next to a % character ?
<?php
$string = 'Get 30% off when you spend over £100 on electronics';
if(strpos($string,'% off') !== false) {
$number = preg_replace("/[^0-9%]/", '', $string);
return $number;
}
this returns 30%100
any help would be great thanks in advance.
This regex will match (and capture) all digits preceding a '%' sign:
'/(\d+)%/'
You can try something like this:
$string = 'Get 30% off when you spend over £100 on electronics';
preg_match('/(\d+)%/', $string, $matches);
print_r($matches[1]);
Do let us know if your requirements are more complex.
Regex:
\d{1,3}%
Explained:
\d{1,3} match a digit [0-9]
Quantifier: {1,3} Between 1 and 3 times, as many times as possible, giving back as needed [greedy]
% matches the character % literally
This seems to do the trick :)
if(strpos($string,'%') !== false) {
$regex_percent = "/((\d{1,5})(?:%))/";
preg_match($regex_percent, $string, $matches_off);
$number = $matches_off[2];
return $number;
}
I'm trying to extract the following pattern {#56DS1e5R9w7v} which is :
{
Hash
a-z, A-Z, 0-9 ( not necessarily an alphanumeric string )
}
Any ideas please?
Thank you
Try this pattern:
\{#([^}]*)\}
It should match all characters that are not }, and place the result in a captured group. You may want to change [^}]* to \w* or [A-Za-z0-9]* if that's problematic.
Example (also on ideone.com):
$str = "hello {#56DS1e5R9w7v} good people";
preg_match_all("/\{#([^}]*)\}/", $str, $matches);
Something like ({#[a-z0-9]+?}) ?
preg_match('/(\{#[a-z0-9]+?\})/i', $sString)
What about:
<?php
$code = '{#56DS1e5R9w7v}';
$matches = preg_match('/^\{#[a-zA-Z0-9]+\}$/', $code);
?>
This makes sure the string begins with a { the second char must be a #, the 3th char until } must be alpha numeric and it must end with a }.
Hope this helped!
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);
}