拆分/解析php表单数据一个输入

I don't know how to parse this data with this format :

name<space>age<space>city

when the name and the city can contain more than one word and can handle when user type 22 yr or 2 yr

I'd really appreciate some assistance or direction on this by anybody.

stephanie brown 22 AAA BBB

OR

stephanie brown 22 year AAA BBB

OR

stephanie brown 22 yr AAA BBB

the output like :

Name : Stephanie Brown
Age  : 22
City : AAA BBB

Your best bet is to convert the input to an array with explode using space as your delimiter.

Then use a state to determine if your parsing in name, age, address. The age is numeric so its easy to move from name->age state, and then the next value after age should be the address unless it has the value of "year | yr | yrs". It won't be 100% perfect but it will work.

try this:-

$string = 'stephanie brown 22 year AAA BBB';//your input string
$patterns = array();
$patterns[0] = '/year/';
$patterns[1] = '/yr/';

$year_removed_string=preg_replace($patterns, "", $string);
$keywords = preg_split("/[\s]+/", $year_removed_string);

print_r($keywords)

here name and city should be one word or if two word then not separated by space.Though it is not perfect answer but can be helpful too. or it works if input is like this :

stephanie_brown  22 year AAA_BBB