codeigniter 2 - Text abbreviation from a string in PHP -
i trying create webshop scratch using codeigniter 2.x. store have products it's own manufacturers such glock, colt, kalashnikov, boker, cz, kizlyar, groundzero, etc...
i need create unique sku (stock keeping unit) name each products mixed product.id
, product.manufacturer
. there no problem it, i.e.: sku product id=13
, manufacturer=groundzero
, name=striker ii
becomes: groundzero-0013
i need generate abbreviation sku becomes this: grzr-0013 or gnzr-0013
any solution?
i coded following sku generator depending on specs mentioned, may change $l
make more unique (in way):
echo sku_gen('zeroground', 13, 2).'<br>'; // zrgr-0013 echo sku_gen('zeroground', 13, 3).'<br>'; // zrgrn-0013 echo sku_gen('glock', 14, 3).'<br>'; // glc-0014 echo sku_gen('cz', 15, 3).'<br>'; // cz-0015 echo sku_gen('kizlyar', 20).'<br>'; // kz-0020 function sku_gen($string, $id = null, $l = 2){ $results = ''; // empty string $vowels = array('a', 'e', 'i', 'o', 'u', 'y'); // vowels preg_match_all('/[a-z][a-z]*/', ucfirst($string), $m); // match every word begins capital letter, added ucfirst() in case there no uppercase letter foreach($m[0] $substring){ $substring = str_replace($vowels, '', strtolower($substring)); // string lower case , remove vowels $results .= preg_replace('/([a-z]{'.$l.'})(.*)/', '$1', $substring); // extract first n letters. } $results .= '-'. str_pad($id, 4, 0, str_pad_left); // add id return $results; }
Comments
Post a Comment