简体   繁体   English

PHP正则表达式检查匹配的给定URL

[英]PHP regex to check the matching given URL

How do you check if a given URL matches at least a sitename? 如何检查给定的URL是否至少与站点名称匹配?

I have: 我有:

$url_to_match = 'http://sub.somesite.com/';

I want to say "MATCH found" for input starting with http://sub.somesite.com only. 我只想输入http://sub.somesite.com来输入“ MATCH found”。

Any help would be very much appreciated. 任何帮助将不胜感激。

Use PHP's parse_url() : 使用PHP的parse_url()

$url = 'http://sub.somesite.com/';
if ('sub.somesite.com' === parse_url($url, PHP_URL_HOST)) {
    // we have a match
}

use parse_url 使用parse_url

example: 例:

function match_url($base,$input)
{
    $base_host = parse_url($base,PHP_URL_HOST);
    $input_host = parse_url($input,PHP_URL_HOST);
    if( $base_host === $input_host ) {
        return true;
    }
    else
    {
        return false;
    }   
}
$base_url = 'http://sub.somesite.com';
$input_url = 'http://sub.somesite.com//bla/bla';
echo (match_url($base_url,$input_url)) ? "URL matched" : "URL mismatched";

I think you need to tell us what you are doing since this request makes no design sense. 我认为您需要告诉我们您在做什么,因为此请求没有任何设计意义。

However, to answer the question. 但是,要回答这个问题。

if(strpos($url_to_match, 'http://sub.anothersite.com/bla') !== FALSE) print 'bad string';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM