$re = '/(?:https?:\/\/)?(?:www\.)?facebook\.com\/(?:(?:\w)*#!\/)?(?:pages\/)?(?:[\w\-]*\/)*?(?:profile.php\?id=(?=\d.*))?([\w\-]*)?(?:[\w\-\.]*)?/';
$str = 'https://facebook.com/vietphu.nguyen.7?rc=p&refid=52&__tn__=R
https://facebook.com/shunchina?rc=p&refid=52&__tn__=R
https://facebook.com/phuocnguyen.nguyen.16568?rc=p&refid=52&__tn__=R
https://facebook.com/profile.php?id=100004404017311&rc=p&refid=52&__tn__=R';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php