php 使用 preg_match 检测 url?字符串中没有 http://
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/2762260/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detecting a url using preg_match? without http:// in the string
提问by Stefan P
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.
我想知道如何根据 preg_match 检查分解为数组的字符串,以查看它是否以 www 开头。我已经有一个检查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();
     }
     }
Thanks! Stefan
谢谢!斯特凡
回答by Igor Serebryany
You want something like:
你想要这样的东西:
%^((https?://)|(www\.))([a-z0-9-].?)+(:[0-9]+)?(/.*)?$%i
this is using the | to match either http://or wwwat the beginning. I changed the delimiter to %to avoid clashing with the |
这是使用 | 匹配http://或www在开头。我更改了分隔符%以避免与|
回答by mromaine
John Gruber of Daring Fireball has posted a very comprehensive regex for all types of URLs that may be of interest. You can find it here:
Daring Fireball 的 John Gruber 为可能感兴趣的所有类型的 URL 发布了一个非常全面的正则表达式。你可以在这里找到它:
http://daringfireball.net/2010/07/improved_regex_for_matching_urls
http://daringfireball.net/2010/07/improved_regex_for_matching_urls
回答by John Smith
I explode the string at first as the url might be half way through it e.g. hello how are you www.google.com
我首先分解字符串,因为 url 可能是它的一半,例如 hello how are you www.google.com
Explode the string and use a foreachstatement.
分解字符串并使用foreach语句。
Eg:
例如:
$string = "hello how are you www.google.com";
$string = explode(" ", $string);
foreach ($string as $word){
  if ( (strpos($word, "http://") === 0) || (strpos($word, "www.") === 0) ){
  // Code you want to excute if string is a link
  }
}
Note you have to use the ===operator because strposcan return, will return a 0which will appear to be false.
请注意,您必须使用===运算符,因为strposcan return, will return a 0,它看起来是false.
回答by John Smith
I used this below which allows you to detect url's anywhere in a string. For my particular application it's a contact form to combat spam so no url's are allowed. Works very well.
我在下面使用了它,它允许您检测字符串中任何位置的 url。对于我的特定应用程序,它是用于打击垃圾邮件的联系表格,因此不允许使用任何网址。效果很好。
Link to resource: https://css-tricks.com/snippets/php/find-urls-in-text-make-links/
资源链接:https: //css-tricks.com/snippets/php/find-urls-in-text-make-links/
My implementation;
我的实现;
<?php
// Validate message
if(isset($_POST['message']) && $_POST['message'] == 'Include your order number here if relevant...') {
$messageError = "Required";
} else {
$message = test_input($_POST["message"]);
}
if (strlen($message) > 1000) {
$messageError = "1000 chars max";
}
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
if (preg_match($reg_exUrl, $message)) {
$messageError = "Url's not allowed";
}
// Validate data
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
回答by Hawkcannon
Try implode($myarray, '').strstr("www.")==0. That implodes your array into one string, then checks whether www.is at the beginning of the string (index 0).
试试implode($myarray, '').strstr("www.")==0。这会将您的数组内爆为一个字符串,然后检查是否www.位于字符串的开头(索引 0)。

