Filter between characters of string in PHP -


i have string this: 12pum4

it has 2 3 numbers @ beginning, has 3 characters in middle , has 1 2 numbers @ end.

i want split somehow 3 section:

  1. the numbers until characters
  2. the characters middle
  3. the rest behind characters.

can please?

you can use preg_match();

$str = '12pum4'; $matches = array(); preg_match('/([0-9]+)([a-za-z]+)(.*)/', $str, $matches); print_r($matches); 

output

array ( [0] => 12pum4 [1] => 12 [2] => pum [3] => 4 ) 

when use function split text , put matches in $matches array

  • [0-9]+ - matches numbers @ least 1 or more
  • [a-za-z]+ characters @ least 1 or more
  • .* anything(almost)
  • () - used subpattern put $matches array

more information of how use preg_match can found here

the solution substr() under condtion wrote has 12 digits, 3 characters , 1 or 2 digits in end.

$str = '12pum4'; $matches = array( 0 => substr($str,0, 2), 1 => substr($str, 2, 3) , 2 => substr($str, 5, strlen($str)>6 ? 2 : 1)); print_r($matches); 

output

array ( [0] => 12 [1] => pum [2] => 4 ) 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -