I am trying to validate whether or not a string contains and starts with BA700
. I have tried using the preg_match()
function in PHP but I have not had any luck. My code is below:
preg_match('/^[0-9]{3}-[0-9]{4}-[0-9]{4}$/', $search))
This does not work unfortunately. Any ideas?
UPDATES CODE:
$needle = 'BA700';
$haystack = 'BA70012345';
if (stripos($haystack, $needle)) {
echo 'Found!';
}
This does not work for me either
Try with substr
like
$needle = 'BA700';
$haystack = 'BA70012345';
if(substr($haystack, 0, 4) == $needle) {
echo "Valid";
} else {
echo "In Valid";
}
You can also check regards with the case by changing both of them in either UPPER
or LOWER
like
if(strtoupper(substr($haystack, 0, 4)) == $needle) {
echo "Valid";
} else {
echo "In Valid";
}
Here is how to correctly use stripos
if (stripos($haystack, $needle) !== false) {
echo 'Found!';
}
Maybe I am taking this a little too literally, but:
if (strncmp($string, 'BA700', 5) === 0) {
// Contains and begins with 'BA700'
}
If the BA700
is case-insensitive then:
if (strncasecmp($string, 'ba700', 5) === 0) {
// Contains and begins with 'ba700'
}
There should not be much more to it than that.
The regular expression, in case you want to know, is:
if (preg_match('/^BA700/', $string) === 1) {
// Contains and begins with 'ba700'
}
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.