split the string into array using regular expression in php -



split the string into array using regular expression in php -

i trying split string , place in array using regular expression. string may alter every time need if string match t: need 4774 4848 if string matches both t: , m:, need result as

array( [0] => 4774 4848, [1]=>0448 888 899 )

this code,

if (preg_match("/[t:|m:|mob:|phone:]\w(.*)[\w:]/mi", "t: 4774 4848 m: 0448 888 899", $matches)) print_r($matches);

here output

array ( [0] => t: 4774 4848 m: 0448 888 [1] => 4774 4848 m: 0448 888 )

how can split [1] => 4774 4848 m: 0448 888

[1] => 4774 4848, [2]=>0448 888 899

please help me in getting this. in advance!

try matching instead of splitting. utilize preg_match_all global match.

preg_match_all("/(?:[tm]:|mob:|phone:)\s*\k.*?(?=\s*[a-z]:|$)/mi", "t: 4774 4848 m: 0448 888 899", $matches); print_r($matches);

output:

array ( [0] => array ( [0] => 4774 4848 [1] => 0448 888 899 ) )

input t: only,

preg_match_all("/(?:[tm]:|mob:|phone:)\s*\k.*?(?=\s*[a-z]:|$)/mi", "t: 4774 4848", $matches); print_r($matches);

output:

array ( [0] => array ( [0] => 4774 4848 ) )

php arrays regex string

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -