How can I check this in php?
$var1 = "pluto_saa1";
$var2 = "pluto_sab1";
$var3 = "pluto_sac1";
$var4 = "pluto_sad1";
$var5 = "pluto_test";
For example, var1, var2, ... var5 contains the s
followed by two unknown letters and ending with 1
but var5 contain something undesired.
Could you help me?
That should do it:
/s[a-zA-Z]{2}1/
2 upper or lower case letters followed by an 1.
you can use this regex
/s[a-zA-Z]{2}1/
[a-zA-Z]
would match a single uppercase or lowercase letter
{n}
is a quantifier which matches preceding pattern n times
Try this
$patterns = '/pluto_s(.*?)1/is';
$var1 = "pluto_saa1";
$var2 = "pluto_sab1";
$var3 = "pluto_sac1";
$var4 = "pluto_sad1";
$var5 = "pluto_test";
$array = array($var1, $var2, $var3, $var4, $var5);
foreach($array as $data)
{
if( preg_match($patterns, $data, $matches))
{
var_dump($matches);
}
}
This will output
array (size=2)
0 => string 'pluto_saa1' (length=10)
1 => string 'aa' (length=2)
array (size=2)
0 => string 'pluto_sab1' (length=10)
1 => string 'ab' (length=2)
array (size=2)
0 => string 'pluto_sac1' (length=10)
1 => string 'ac' (length=2)
array (size=2)
0 => string 'pluto_sad1' (length=10)
1 => string 'ad' (length=2)
function check_str($str) {
$pattern="%_s+[a-z]{2}1$%";
if(preg_match($pattern,$str)) {
echo 'ok';
}
}
check_str($var1);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.