如何从PHP中的preg_match_all结果中修剪空格?

Given the function:

function getUrlsAndEmails($string) {
    $regex = '/(?:[^\s]+@[a-z]+(\.[a-z]+)+)|(?:(?:(?:[a-z]+:\/\/)|\s)[a-z]+(\.[a-z]+)+(\/[^\s]*)?)/';
    preg_match_all($regex, $string, $matches);
    return ($matches[0]);
}

Sometimes return results like:

Array
(
    [0] => google.com
    [1] =>  yahoo.com
)

How can I efficiently trim whitespace from all results of a preg_match_all()?

Of course I can loop through all of the results and trim(), but is there a more efficient way than adding this to the function above:

foreach ($matches[0] as $k => $v) {
    $matches[0][$k] = trim($v);
}

Try this:

$regex = '/(?:[^\s]+@[a-z]+(\.[a-z]+)+)|(?:(?:(?:[a-z]+:\/\/)|(?!\s))[a-z]+(\.[a-z]+)+(\/[^\s]*)?)/';

It uses a negative lookahead assertion for the space.