i havea series of strings in following format...
aebvaejbh286720467-snehndfn3984777-sobn3895 jklasebntb-nlb93876-adbnbnbn-34978 357896-alalalalalal3986-bh3806-023847 and on.
i need letters , dashes, first 2 numbers in group, example:
aebvaejbh286720467-snehndfn3984777-sobn3895 would become
aebvaejbh28-snehndfn39-sobn38 and
jklasebntb-nlb93876-adbnbnbn-34978 would become
jklasebntb-nlb93-adbnbnbn-34 and
357896-alalalalalal3986-bh3806-023847 would become
35-alalalalalal39-bh38-02 and on.
i think sed/awk in bash, need in php. ideas great.
i build new string , add each character 1 @ time while keeping count how many numbers i've added. if character not number, add - if is, add if it's 1st or 2nd number.
$string = '357896-alalalalalal3986-bh3806-023847'; $result = ''; $digit_count = 0; foreach(str_split($string) $character) { if(!is_numeric($character) || ++$digit_count <= 2) $result .= $character; if($character == '-') $digit_count = 0; } print $result;
Comments
Post a Comment