Specifically, it should be 6 or more alphanumerics (0-9 + a-z).
The second character is a letter.
The third character is an odd number.
Any help?
An example regex that matches this for ASCII is
^[0-9A-Za-z][A-Za-z][13579][0-9A-Za-z]{3,}$
PHP code
<?php
$test = '0A1000';
if (preg_match('/^[0-9A-Za-z][A-Za-z][13579][0-9A-Za-z]{3,}$/', $test)) {
// Do some stuff
echo "matched";
}
Related
If the first character of my string contains any of the following letters, then I would like to change the first letter to Uppercase: (a,b,c,d,f,g,h,j,k,l,m,n,o,p,q,r,s,t,v,w,y,z) but not (e,i,u,x).
For example,
luke would become Luke
egg would stay the same as egg
dragon would become Dragon
I am trying to acheive this with PHP, here's what I have so far:
<?php if($str("t","t"))
echo ucfirst($str);
else
echo "False";
?>
My code is simply wrong and it doesn't work and I would be really grateful for some help.
Without regex:
function ucfirstWithCond($str){
$exclude = array('e','i','u','x');
if(!in_array(substr($str, 0, 1), $exclude)){
return ucfirst($str);
}
return $str;
}
$test = "egg";
var_dump(ucfirstWithCond($test)); //egg
$test = "luke";
var_dump(ucfirstWithCond($test)); //Luke
Demo:
http://sandbox.onlinephpfunctions.com/code/c87c6cbf8c616dd76fe69b8f081a1fbf61cf2148
You may use
$str = preg_replace_callback('~^(?![eiux])[a-z]~', function($m) {
return ucfirst($m[0]);
}, $str);
See the PHP demo
The ^(?![eiux])[a-z] regex matches any lowercase ASCII char at the start of the string but e, u, i and x and the letter matched is turned to upper inside the callback function to preg_replace_callback.
If you plan to process each word in a string you need to replace ^ with \b, or - to support hyphenated words - with \b(?<!-) or even with (?<!\S) (to require a space or start of string before the word).
If the first character could be other than a letter then check with an array range from a-z that excludes e,i,u,x:
if(in_array($str[0], array_diff(range('a','z'), ['e','i','u','x']))) {
$str[0] = ucfirst($str[0]);
}
Probably simpler to just check for the excluded characters:
if(!in_array($str[0], ['e','i','u','x'])) {
$str[0] = ucfirst($str[0]);
}
I am trying to find an integer in a string that has the following characteristics:
- Is exactly 8 digits long
- Is between 21000000 and 22000000
- Or between 79000000 and 79999999
I want any number between those ranges to be redacted.
I tried using preg_replace. I'm not sure which pattern to use for this function.
I would suggest this:
preg_replace('/(^|[^0-9]{1})(21[0-9]{6}|22000000|79[0-9]{6})([^0-9]{1}|$)/', '$1 |$2| $3', $str);
// (^|[^0-9]{1}) - set bordering character as non-numeric
// (21[0-9]{6}|22000000|79[0-9]{6}) - match the numbers range you need
// ([^0-9]{1}|$) make sure it doesn't include any other numbers
NB! Make sure you include $1 and $3 in your replace string, otherwise you'll lose chars surrounding the number.
try
<?php
$str ="sdsds21000021dsds";
$int = filter_var($str, FILTER_SANITIZE_NUMBER_INT);
if($int){
$number_of_digits = strlen((string)$int);
if($number_of_digits == 8){
if((($int >= 21000000)&&($int <=22000000))||(($int >= 79000000)&&($int <=79999999))){
echo $int;
} else { // not found }
} else { // not found }
} else { // not found }
hope it helps :)
I've managed to build a regular expression based on what you need.
Hope it helps!
\b21[0-9]{6}\b|\b79[0-9]{6}\b
\b is word boundary
{number} is repetition count
21 is interpreted literally, as 79 is
[0-9] matches exactly one number in 0-9 range
test it here please if you need to tweak it.
https://regex101.com/
I have a regular expression which compares if a string is having both alpha and numerical values. But i need to compare if the string is having any special characters and length of the string should be 6.
my current regular expression is
$val = 'A457718';
preg_match('/^[A-Z]|[0-9A-Z]*([0-9][A-Z]|[A-Z][0-9])[0-9A-Z]*$/i', $val)
But i need to compare if there are any special characters are there and string length should be 6. Any help would be greatly appreciated.
You don't need a regex to check for string that is 6 characters long and only numerical.
$val = 'A457718';
if (is_numeric($val) && strlen($val) == 6) {
echo 'true';
} else {
echo 'false';
}
Demo: http://sandbox.onlinephpfunctions.com/code/25c426d1bbbfce4a96c8c1ba74cc4a84b66c2435
Functions:http://php.net/manual/en/function.is-numeric.phphttp://php.net/manual/en/function.strlen.php
If for some reason you require it in regex.
preg_match('~^\d{6}$~', $val);
Demo: https://regex101.com/r/oR9bT4/1
The pattern /^[a-zA-Z0-9]{6}$/ will match six characters that are alphanumeric.
if(preg_match('/^[a-zA-Z0-9]{6}$/', 'A45BBB')){
// Is valid
}
for the length, put at the end {0,6} .. this will limit string to be from 0 to 6 characters
This would match any 6-length alphanumeric string:
\b[a-zA-Z0-9]{6}\b
or including the underscore character:
\b\w{6}\b
This should be clarified:
But i need to compare if there are any special characters
I'm trying to write a regular expression which could match a string that possibly includes Chinese characters. Examples:
hahdj5454_fd.fgg"
example.com/list.php?keyword=关键字
example.com/list.php?keyword=php
I am using this expression:
$matchStr = '/^[a-z 0-9~%.:_\-\/[^x7f-xff]+$/i';
$str = "http://example.com/list.php?keyword=关键字";
if ( ! preg_match($matchStr, $str)){
exit('WRONG');
}else{
echo "RIGHT";
}
It matches plain English strings like that dasdsdsfds or http://example.com/list.php, but it doesn't match strings containing Chinese characters. How can I resolve this?
Assuming you want to extend the set of letters that this regex matches from ASCII to all Unicode letters, then you can use
$matchStr = '#^[\pL 0-9~%.:_/-]+$#u';
I've removed the [^x7f-xff part which didn't make any sense (in your regex, it would have matched an opening bracket, a caret, and some ASCII characters that were already covered by the a-z and 0-9 parts of that character class).
This works:
$str = "http://mysite/list.php?keyword=关键字";
if (preg_match('/[\p{Han}]/simu', $str)) {
echo "Contains Chinese Characters";
}else{
exit('WRONG'); // Doesn't contains Chinese Characters
}
I don't want preg_match_all ... because the form field only allows for numbers and letters... just wondering what the right syntax is...
Nothing fancy ... just need to know the right syntax for a preg_match statement that looks for only numbers and letters. Something like
preg_match('/^([^.]+)\.([^.]+)\.com$/', $unit)
But that doesn't look for numbers too....
If you just want to ensure a string contains only alphanumeric characters. A-Z, a-z, 0-9 you don't need to use regular expressions.
Use ctype_alnum()
Example from the documentation:
<?php
$strings = array('AbCd1zyZ9', 'foo!#$bar');
foreach ($strings as $testcase) {
if (ctype_alnum($testcase)) {
echo "The string $testcase consists of all letters or digits.\n";
} else {
echo "The string $testcase does not consist of all letters or digits.\n";
}
}
?>
The above example will output:
The string AbCd1zyZ9 consists of all letters or digits.
The string foo!#$bar does not consist of all letters or digits.
if(preg_match("/[A-Za-z0-9]+/", $content) == TRUE){
} else {
}
If you want to match more than 1, then you'll need to, however, provide us with some code and we can help better.
although, in the meantime:
preg_match("/([a-zA-Z0-9])/", $formContent, $result);
print_r($result);
:)