Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.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正则表达式匹配给定Url中的域名_Php_Regex - Fatal编程技术网

PHP正则表达式匹配给定Url中的域名

PHP正则表达式匹配给定Url中的域名,php,regex,Php,Regex,我想要的是给定域是否以字符串形式存在 我的问题的例子有 +----------------------------------------------+-----------------------+ | input | output | +----------------------------------------------+-----------------------+ | h

我想要的是给定域是否以字符串形式存在

我的问题的例子有

+----------------------------------------------+-----------------------+
| input                                        | output                |
+----------------------------------------------+-----------------------+
| http://www.example.com/questions/ask         | match or true         |
| http://example.com/check                     | match or true         |
| http://www.google.com/ig/moduleurl           |
|    =http%3A%2F%2Fwww.example.com%2Fquestion  | false                 |
| http://example.com/search/%25C3%25A9t%25     | match true            |
+----------------------------------------------+-----------------------+
任何帮助都是值得的


谢谢

这里不需要正则表达式IMO:

使用
parse\u url()
,您可以获得域、主机。。。你想要的一切,真的。再加上(极快的)字符串函数:

if (strstr(parse_url($input,PHP_URL_HOST),'example.com'))
{
    echo $input.' is a match';
}
但在您的场景中,最快的方法是:

$match = strpos($input, 'example.com');
$match = $match !== false && $match <= 12 ? true : false;
//12 is max for https://www.example.com

这是我能想到的最快的方法

这里不需要regex,我想:

使用
parse\u url()
,您可以获得域、主机。。。你想要的一切,真的。再加上(极快的)字符串函数:

if (strstr(parse_url($input,PHP_URL_HOST),'example.com'))
{
    echo $input.' is a match';
}
但在您的场景中,最快的方法是:

$match = strpos($input, 'example.com');
$match = $match !== false && $match <= 12 ? true : false;
//12 is max for https://www.example.com

是我能想到的最快的方法

您可以使用以下模式:

$pattern = '~^(?:ht|f)tps?://[^/]*?(?<=\.|/)'.preg_quote($domain).'(?=/|$)~i';

$pattern='~^(?:ht | f)tps?://[^/]*?(?您可以使用此模式:

$pattern = '~^(?:ht|f)tps?://[^/]*?(?<=\.|/)'.preg_quote($domain).'(?=/|$)~i';
$pattern='~^(?:ht | f)tps?://[^/]*?(?使用比解析整个URL更好:

$arr = parse_url($url);
$host = $arr['host'];

// now just match the hostname
if (preg_match('#^(?:[^.]+\.)*example\.com$#i', $host, $arr))
    var_dump($arr); // or return true;
此正则表达式也可以工作:

if (preg_match('#^https?://(?:[^.]+\.)*example\.com/#i', $url, $arr))
   var_dump($arr); // return true;
更好地使用而不是解析整个URL:

$arr = parse_url($url);
$host = $arr['host'];

// now just match the hostname
if (preg_match('#^(?:[^.]+\.)*example\.com$#i', $host, $arr))
    var_dump($arr); // or return true;
此正则表达式也可以工作:

if (preg_match('#^https?://(?:[^.]+\.)*example\.com/#i', $url, $arr))
   var_dump($arr); // return true;

为什么你需要一个正则表达式?php等价的
instr
/
index
难道不够吗?肮脏的方式:搜索
“/[\w.]*example.com/”
?@Kent:url匹配的困难在于区分
权限
查询
部分(使用)。查询部分可能包含url本身(想想重定向目标)这不是正则表达式的工作,而是你选择的语言中的现有工具的工作。正则表达式并不是一根魔杖,当你遇到每一个涉及字符串的问题时,你都会挥舞它我们希望使用已经编写、测试和调试过的现有代码。在PHP中,使用函数。Perl:.Ruby:…NET:为什么需要正则表达式?PHP等价的
instr
/
index
就够了吗?肮脏的方式:搜索
“/[\w.]*example.com/”
?@Kent:url匹配的困难在于区分
权限
查询
部分(使用)。查询部分可能包含url本身(考虑重定向目标)这不是正则表达式的工作,而是你选择的语言中的现有工具的工作。正则表达式并不是一根魔杖,当你遇到每一个涉及字符串的问题时,你都会挥舞它您希望使用已经编写、测试和调试的现有代码。在PHP中,使用函数。Perl:.Ruby:…NET:这将匹配查询字符串中是否存在相关url。至少使用
^https://(?:[^.]+\)*示例\.com/#i
(插入符号标记测试字符串的开始)@collapsar:谢谢,更正,还添加了基于解析url的解决方案。这将匹配查询字符串中相关url的存在。请至少使用
^https:/(?:[^.]+\)*example\.com/\code>(插入符号标记测试字符串的开始)@collapsar:谢谢,更正,还添加了基于解析url的解决方案。