简体   繁体   English

如何检查文本中是否包含特定域名?

[英]How to check if a text contains a specific domain name?

I currently have this but it's not flawless: 我目前有这个,但这并不是完美的:

$testcases = array(
array("I love mywebsite.com", true),
array("mywebsite.com/ is what I like", true),
array("www.mywebsite.com is my website", true),
array("Check out www.mywebsite.com/", true),
array("... http://mywebsite.com ...", true),
array("... http://mywebsite.com/ ...", true),
array("... http://www.mywebsite.com ...", true),
array("... http://www.mywebsite.com/ ...", true),
array("I like commas and periods. Just like www.mywebsite.com, they do it too!", true),
array("thisismywebsite.com is a lot better", false),
array("The URL fake.mywebsite.com is unknown to their server", false),
array("Check out http://redirect.mywebsite.com/www.ultraspammer.com", false)
);

function contains_link($text) {
return preg_match("/(https?:\/\/(?:www\.)?|(?:www\.))mywebsite\.com/", $text) > 0;
}

foreach ($testcases as $case) {
echo $case[0] . "=".(contains_link($case[0]) ? "true" : "false") . " and it should be " . ($case[1] ? "true" : "false") . "<br />";
}

Output: 输出:

I love mywebsite.com=false and it should be true
mywebsite.com/ is what I like=false and it should be true
www.mywebsite.com is my website=true and it should be true
Check out www.mywebsite.com/=true and it should be true
... http://mywebsite.com ...=true and it should be true
... http://mywebsite.com/ ...=true and it should be true
... http://www.mywebsite.com ...=true and it should be true
... http://www.mywebsite.com/ ...=true and it should be true
I like commas and periods. Just like www.mywebsite.com, they do it too!=true and it should be true
thisismywebsite.com is a lot better=false and it should be false
The URL fake.mywebsite.com is unknown to their server=false and it should be false
Check out http://redirect.mywebsite.com/www.ultraspammer.com=false and it should be false

An alternative to regex: parse_url() 正则表达式的替代方法: parse_url()

$url = parse_url($text);
if($url['host'] == 'www.mywebsite.com' || $url['host'] == 'mywebsite.com')

UPDATE: 更新:

Assuming that $text can have a lot of domains,use strstr() instead. 假设$text可以有很多域,请改用strstr()

if(strstr($text,"mywebsite.com") !== FALSE)

UPDATE 2: 更新2:

function contains_link($text) {
        return preg_match("/(^(https?:\/\/(?:www\.)?|(?:www\.))?|\s(https?:\/\/(?:www\.)?|(?:www\.))?)mywebsite\.com/", $text);
}

and: 和:

  contains_link("AAAAAAA http://mywebsite.com"); //1
  contains_link("foo BAaa http://www.mywebsite.com"); //1
  contains_link("abc.com www.mywebsite.com"); // 1

I think what you're looking for is this: 我认为您正在寻找的是:

^(https?://)?(www\\.)?mywebsite\\.com/?

See it here in action: http://regexr.com?30t6m 观看此处操作: http : //regexr.com?30t6m


Here it is in PHP: 在PHP中:

function contains_link($text) {
    return preg_match("~^(https?://)?(www\.)?mywebsite\.com/?~", $text);
}

PS If you want to be sure that there's nothing after it, you should append a $ to the end. 注释:如果要确保后面没有任何内容,则应在末尾附加一个$

if you only search for the text: 如果仅搜索文本:

strpos($text, "mywebsite.com") !== FALSE

if you want to seach for an exact "word" (start): 如果您想搜索一个确切的“单词”(开始):

preg_match("/(^|\s)(https?:\/\/)?(www\.)?mywebsite\.com/", $text);

or (start & end): 或(开始和结束):

preg_match("/(^|\s)(https?:\/\/)?(www\.)?mywebsite\.com\/?(\s|[,.]|$)/", $text);

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

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