简体   繁体   中英

How can I trim whitespace from results of preg_match_all in PHP?

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.

Use map('trim') .

<?php
$pattern = Pattern::of('(?:[^\s]+@[a-z]+(\.[a-z]+)+)|(?:(?:(?:[a-z]+:\/\/)|\s)[a-z]+(\.[a-z]+)+(\/[^\s]*)?)');
$matcher = $pattern->match($string);
   
var_dump($matcher->map('trim'));

result

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM