Skip to content
Advertisement

Custom Regular Expressions [closed]

How is it possible to use regex to separate

BCT34385Z0000N07518Z
BCT34395Z0000N07518Z

into BCT343 format? I am using this for magento to break 2 types of serial number ie BCT34385Z0000N07518Z and BCT34395Z0000N07518Z into regex to just identified the first 6 characters ie BCT343.

Advertisement

Answer

This is very bad practice, but because you asked for it:

$str = 'BCT34385Z0000N07518Z';
preg_match('/^(.{6})(.*?)$/', $str, $result);

echo $result[1]; // 'BCT343'
echo $result[2]; // '85Z0000N07518Z'

or if you want an if statement:

$str = ...;

if (preg_match('/^BCT343/', $str)) {
    // yes!
}
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement