php - Separating the non-numeric part of an alphanumeric string -
i have set of alphanumeric strings, each beginning non-numeric characters , ending numbers, want able separate non-numeric parts numeric parts, assuming number of characters constant each string, have been easy have strings abc123, abcd456, etc.
i this:
<?php $strarray = array(); $strarray[] = '123abc'; $strarray[] = 'abc123'; $strarray[] = 'abcd456'; $strarray[] = 'abc4567'; $strarray[] = 'abcd4567'; foreach ($strarray $str){ preg_match('/^([a-za-z]+)([0-9]+)$/', $str, $matches); if (count($matches)===3){ $alphapart = $matches[1]; $numericpart = $matches[2]; var_dump($alphapart); var_dump($numericpart); } else{ echo 'no match: ' . $str; } } ?>
i prefer more specific definition of pattern for. , suspect preg_split worse approach, performance wise. @ least, consider overkill if there 2 parts split string int0.
Comments
Post a Comment