使用 preg_match 检测 url?字符串中没有 http:// [英] Detecting a url using preg_match? without http:// in the string

查看:34
本文介绍了使用 preg_match 检测 url?字符串中没有 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 检测 url?字符串中没有 http://的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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