使用我的代码,如何只为那些在preg_match_all和正则表达式中有10个字符的用户获取结果数组?
这是我的代码
$data = 'Maria address is QwMP_jkRkM and lives in Peru, Joseph address is QMPjkRk2ZM and lives in Peru, Miguel address is Q.wMP_jkRljo_hkM and lives in New York, George address is hdiJoW58_7 and lives in Austria';
preg_match_all('#(.*?) address is (.*?) and lives in (.*?)#', $data, $output);
实际上返回所有匹配项,我需要删除其地址中包含超过10个字符的结果。
注意:我不应该使用foreach
约束似乎是人为的,但这应该产生正确的输出(如果不方便),它确实使用(.*?)
:
$data = 'Maria address is QwMP_jkRkM and lives in Peru, Joseph address is QMPjkRk2ZM and lives in Peru, Miguel address is Q.wMP_jkRljo_hkM and lives in New York, George address is hdiJoW58_7 and lives in Austria';
preg_match_all('#([^ ]*?) address is (.{1,10}) and lives in (.*?)(?:$|,)#', $data, $output);
print_r($output);
结果:
Array
(
[0] => Array
(
[0] => Maria address is QwMP_jkRkM and lives in Peru,
[1] => Joseph address is QMPjkRk2ZM and lives in Peru,
[2] => George address is hdiJoW58_7 and lives in Austria
)
[1] => Array
(
[0] => Maria
[1] => Joseph
[2] => George
)
[2] => Array
(
[0] => QwMP_jkRkM
[1] => QMPjkRk2ZM
[2] => hdiJoW58_7
)
[3] => Array
(
[0] => Peru
[1] => Peru
[2] => Austria
)
)