[英]PHP (Regex) Check if a string contains at least 4 digits + 2 letters
[英]PHP regex - check if string contains a given value with certain count of random digits in the end
我在数组中只有几个字符串:
$array = array('BE001 FIRST', 'BE01 SECOND', 'SV001 THIRD');
foreach ($array as $item) {
// preg_match('', $item);
}
我想得到一个数组元素,如果它紧随其后包含“ BE” + 3个数字。 在这种情况下,第一个数组元素。
我对regex
并不熟悉,但是我看到了一些示例,该示例如何匹配给定值,但不匹配给定值,并最终匹配一定数量的随机数。 请帮我!
使用preg_grep
:
$array = array('BE001 FIRST', 'BE01 SECOND', 'SV001 THIRD');
$res = preg_grep('/^BE\d{3}\b/', $array);
print_r($res);
输出:
Array
(
[0] => BE001 FIRST
)
这是我的版本:
<?php
$input = ['BE001 FIRST', 'BE01 SECOND', 'SV001 THIRD', 'BE1234 FOURTH'];
$output = [];
array_walk($input, function(&$entry) use (&$output) {
if (preg_match('/BE\d{3}([^\d]|$)/', $entry)) {
$output[] = $entry;
}
});
print_r($output);
输出显然是:
Array
(
[0] => BE001 FIRST
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.