使用正则表达式将字符串中的 URL 放入数组中(尾随句点问题) [英] Put URLs from string into array using regex (problem with trailing period)

查看:14
本文介绍了使用正则表达式将字符串中的 URL 放入数组中(尾随句点问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个函数,从字符串中提取所有 url,并从末尾删除潜在的斜杠.

I am trying to write a function that pulls all url's from a string and remove a potential trailing slash from the end.

function getUrls($string) {
    $regex = '/https?\:\/\/[^\" ]+/i';
    preg_match_all($regex, $string, $matches);
    return ($matches[0]);
}

但这会返回 http://test.com.(尾随期)如果我有

But that returns http://test.com. (trailing period) If i have

$string = "Hi I am sharing http://test.com.";
$urls = getUrls($string);

返回以句点结尾的 URL.

It returns the URL with the period at the end.

推荐答案

这个似乎有效(摘自 这里)

This one seems to work (taken from here)

$regex="/(https?:\/\/+[\w\-]+\.[\w\-]+)/i";

这篇关于使用正则表达式将字符串中的 URL 放入数组中(尾随句点问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆