使用preg_match检测网址?字符串中不含http:// [英] Detecting a url using preg_match? without http:// in the string
本文介绍了使用preg_match检测网址?字符串中不含http://的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道如何根据preg_match检查分成数组的字符串,看看它是否以www开头.我已经有一个可以检查 http://www 了.
I was wondering how I could check a string broken into an array against a preg_match to see if it started with www. I already have one that check for http://www.
function isValidURL($url)
{
return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url);
}
$stringToArray = explode(" ",$_POST['text']);
foreach($stringToArray as $key=>$val){
$urlvalid = isValidURL($val);
if($urlvalid){
$_SESSION["messages"][] = "NO URLS ALLOWED!";
header("Location: http://www.domain.com/post/id/".$_POST['postID']);
exit();
}
}
谢谢! 斯蒂芬
推荐答案
您想要类似的东西
%^((https?://)|(www\.))([a-z0-9-].?)+(:[0-9]+)?(/.*)?$%i
这是使用|在开头匹配http://
或www
.我将分隔符更改为%
,以避免与|
this is using the | to match either http://
or www
at the beginning. I changed the delimiter to %
to avoid clashing with the |
这篇关于使用preg_match检测网址?字符串中不含http://的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文