检查字符串是否是URL [英] check if a string is a URL

查看:93
本文介绍了检查字符串是否是URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过很多问题,但无法理解它是如何工作的 我想要一个更简单的案例.

I've seen many questions but wasn't able to understand how it works as I want a more simple case.

如果我们有文本,无论文本是什么,我都想检查一下它是否是URL.

If we have text, whatever it is, I'd like to check if it is a URL or not.

$text = "something.com"; //this is a url

if (!IsUrl($text)){
    echo "No it is not url";
    exit; // die well
}else{
    echo "Yes it is url";
    // my else codes goes
}

function IsUrl($url){
    // ???
}

在阻止JS的情况下,除了检查JavaScript以外,还有其他方法吗?

Is there any other way rather than checking with JavaScript in the case JS is blocked?

推荐答案

,但示例URL过于简化,(\w+)\.(\w+)将与之匹配.有人提到了filter_var,它只是一个filter_var($url, FILTER_VALIDATE_URL),但它似乎没有像非ascii字符一样,要当心...

but your example URL is over simplified, (\w+)\.(\w+) would match it. somebody else mentioned filter_var which is simply a filter_var($url, FILTER_VALIDATE_URL) but it doesn't seem to like non-ascii characters so, beware...

这篇关于检查字符串是否是URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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