Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP正则表达式允许mailto:http://和tel:hyperlinks_Php_Regex_Laravel - Fatal编程技术网

PHP正则表达式允许mailto:http://和tel:hyperlinks

PHP正则表达式允许mailto:http://和tel:hyperlinks,php,regex,laravel,Php,Regex,Laravel,我需要做的是允许一个文本字段进行验证,并允许mailto:http://或tel:type的URL 我使用的是PHP5(和Laravel4,但与本文无关) 我在谷歌上搜索了一段时间,但我似乎找不到一个表达式来匹配这三种类型。我尝试了一些长而复杂的字符串,还有一些真正的短字符串,结果返回false 以下是我的最新消息: mailto:([^\?]*)|http:([^\?]*)|tel:([^\?]*) 解决方案: 因为我使用的是Laravel4,所以我决定使用parse_url函数而不是正则表

我需要做的是允许一个文本字段进行验证,并允许mailto:http://或tel:type的URL

我使用的是PHP5(和Laravel4,但与本文无关)

我在谷歌上搜索了一段时间,但我似乎找不到一个表达式来匹配这三种类型。我尝试了一些长而复杂的字符串,还有一些真正的短字符串,结果返回false

以下是我的最新消息:

mailto:([^\?]*)|http:([^\?]*)|tel:([^\?]*)
解决方案:

因为我使用的是Laravel4,所以我决定使用parse_url函数而不是正则表达式。也就是说,还提供了其他一些很好的解决方案

我的最终验证器功能:

    Validator::extend('any_url', function($attribute, $value)
    {
        $allowed = ['mailto', 'http', 'https', 'tel'];
        $parsed = parse_url($value);

        return in_array($parsed['scheme'], $allowed);
    });

您需要将整个正则表达式放在捕获分组中,还需要在
http:
之后添加
/

mailto:([^\?]*)|(http://([^\?]*))|(tel:([^\?]*))
因为在正则表达式中,pip的工作方式不同:

mailto:     #first
([^\?]*)|http: #second
([^\?]*)|tel:  #third
([^\?]*)       #fourth

您可以使用
parse_url
,它将为您提供
方案
。然后检查它是否在
['mailto','http','https','tel']

尝试以下方法:

((mailto:\w+)|(tel:\w+)|(http://\w+)).+

您可能需要:

/^((?:tel|https?|mailto):.*?)$/
示例:

 $strings  = array("http://www.me.com", "mailto:hey@there.nyc", "tel:951261412", "hyyy://www.me.com");

foreach($strings as $string){

if (preg_match('/^((?:tel|https?|mailto):.*?)$/im', $string)) {
    echo $string ."\n";
}else{
echo "No Match for : $string \n";
}
}
^((?:tel|https?|mailto):.*?)$
-----------------------------

Assert position at the beginning of a line (at beginning of the string or after a line break character) (line feed) «^»
Match the regex below and capture its match into backreference number 1 «((?:tel|https?|mailto):.*?)»
   Match the regular expression below «(?:tel|https?|mailto)»
      Match this alternative (attempting the next alternative only if this one fails) «tel»
         Match the character string “tel” literally (case insensitive) «tel»
      Or match this alternative (attempting the next alternative only if this one fails) «https?»
         Match the character string “http” literally (case insensitive) «http»
         Match the character “s” literally (case insensitive) «s?»
            Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
      Or match this alternative (the entire group fails if this one fails to match) «mailto»
         Match the character string “mailto” literally (case insensitive) «mailto»
   Match the character “:” literally «:»
   Match any single character that is NOT a line break character (line feed) «.*?»
      Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Assert position at the end of a line (at the end of the string or before a line break character) (line feed) «$»

说明:

 $strings  = array("http://www.me.com", "mailto:hey@there.nyc", "tel:951261412", "hyyy://www.me.com");

foreach($strings as $string){

if (preg_match('/^((?:tel|https?|mailto):.*?)$/im', $string)) {
    echo $string ."\n";
}else{
echo "No Match for : $string \n";
}
}
^((?:tel|https?|mailto):.*?)$
-----------------------------

Assert position at the beginning of a line (at beginning of the string or after a line break character) (line feed) «^»
Match the regex below and capture its match into backreference number 1 «((?:tel|https?|mailto):.*?)»
   Match the regular expression below «(?:tel|https?|mailto)»
      Match this alternative (attempting the next alternative only if this one fails) «tel»
         Match the character string “tel” literally (case insensitive) «tel»
      Or match this alternative (attempting the next alternative only if this one fails) «https?»
         Match the character string “http” literally (case insensitive) «http»
         Match the character “s” literally (case insensitive) «s?»
            Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
      Or match this alternative (the entire group fails if this one fails to match) «mailto»
         Match the character string “mailto” literally (case insensitive) «mailto»
   Match the character “:” literally «:»
   Match any single character that is NOT a line break character (line feed) «.*?»
      Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Assert position at the end of a line (at the end of the string or before a line break character) (line feed) «$»

那抓取零件呢
(mailto:([^\?]*)|(http:([^\?]*))|(电话:([^\?]*))
如果您只需要验证,您可以使用
filter\u var
filter\u VALIDATE\u URL
作为过滤器。你可以在这里检查一些测试:^^^过滤器^验证^ URL几乎就是答案,但它不支持tel:protocolah,这是个好主意。实际上,在本例中,我使用的是Laravel验证器,它需要字符串…我最终决定使用它,因为它是一个更容易阅读和扩展的解决方案,而且它与Laravel一起工作非常好。我在您的最终答案上面发布了我的最终代码,您没有检查字符串是否没有任何协议,因此不会设置
$parsed['scheme']
<代码>!isset($parsed['scheme'])。