aam*_*dia 83
REGEX是您的问题的答案.采用对象操纵器的答案...所有它缺少的是排除"逗号",所以你可以尝试这个排除它们的代码并给出3个分离的URL作为输出:
$string = "The text you want to filter goes here. http://google.com, https://www.youtube.com/watch?v=K_m7NEDMrV0,https://instagram.com/hellow/";
preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $string, $match);
echo "<pre>";
print_r($match[0]);
echo "</pre>";
Run Code Online (Sandbox Code Playgroud)
而输出是
Array
(
[0] => http://google.com
[1] => https://www.youtube.com/watch?v=K_m7NEDMrV0
[2] => https://instagram.com/hellow/
)
Run Code Online (Sandbox Code Playgroud)
您可以在这里尝试正则表达式:
$string = "The text you want to filter goes here. http://google.com, https://www.youtube.com/watch?v=K_m7NEDMrV0,https://instagram.com/hellow/";
preg_match_all('#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#', $string, $match);
echo "<pre>";
print_r($match[0]);
echo "</pre>";
Run Code Online (Sandbox Code Playgroud)
这给出了以下输出:
Array
(
[0] => http://google.com
[1] => https://www.youtube.com/watch?v=K_m7NEDMrV0,https://instagram.com/hellow/
)
Run Code Online (Sandbox Code Playgroud)
小智 5
请尝试使用以下正则表达式
$regex = '/https?\:\/\/[^\",]+/i';
preg_match_all($regex, $string, $matches);
echo "<pre>";
print_r($matches[0]);
Run Code Online (Sandbox Code Playgroud)
希望这对你有用