如何用PHP检测URL中是否存在特定用户链接?

更新于
2026-09-27 17:02:47
1阅读来源:SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计495个文字,预计阅读时间需要2分钟。

如何用PHP检测URL中是否存在特定用户链接?

方法一:使用 substr_count() 函数格式:substr_count(string, substring, start, length)string:被检查的字符串substring:要搜索的子字符串start:在字符串中开始搜索的位置(可选)length:搜索的长度(可选)

方法一:利用substr_count()函数

a.语法格式:

substr_count(string,substring,start,length)

  • string:被检查的字符串。

    如何用PHP检测URL中是否存在特定用户链接?

  • substring:要搜索的字符串。

  • start:规定在字符串中何处开始搜索。(可选)

  • length:规定搜索的长度。(可选)

返回值:返回子串在字符串中出现的次数。

b. 示例:

<?php $url = 'www.php.cn'; $key = 'cn'; if (substr_count($url, $key) == false) echo 'URL中不存在子字符串'.$key; else echo 'URL中存在子字符串'.$key; echo "<br>"; $key1='com'; if (substr_count($url, $key1) == false) echo 'URL中不存在子字符串 '.$key1.' <br>' ; else echo 'URL中存在 '.$key1.' <br>' ; ?>

c.输出:

URL中存在子字符串cn URL中不存在 com

方法二:利用strpos()函数

a.语法格式:

strpos(string,find,start)

  • string 要搜索的字符串。

  • find 要查找的字符串。

  • start 在何处开始搜索。 (可选)

返回值:查找字符串在另一字符串中第一次出现的位置。

ps:strpos() 函数对大小写敏感。

b.示例:

<?php $url = 'www.php.cn'; $key = 'cn'; if (strpos($url, $key) == false) { echo 'URL中不存在子字符串'.$key.' <br>' ; } else { echo 'URL中存在子字符串 '.$key.' <br>' ; } echo "<br>"; $key1 = 'com'; if (strpos($url, $key1) == false) { echo 'URL中不存在子字符串 '.$key1; } else { echo 'URL中存在子字符串 '.$key1 ; } ?>

c.输出:

URL中存在子字符串 cn URL中不存在子字符串 com

本文共计495个文字,预计阅读时间需要2分钟。

如何用PHP检测URL中是否存在特定用户链接?

方法一:使用 substr_count() 函数格式:substr_count(string, substring, start, length)string:被检查的字符串substring:要搜索的子字符串start:在字符串中开始搜索的位置(可选)length:搜索的长度(可选)

方法一:利用substr_count()函数

a.语法格式:

substr_count(string,substring,start,length)

  • string:被检查的字符串。

    如何用PHP检测URL中是否存在特定用户链接?

  • substring:要搜索的字符串。

  • start:规定在字符串中何处开始搜索。(可选)

  • length:规定搜索的长度。(可选)

返回值:返回子串在字符串中出现的次数。

b. 示例:

<?php $url = 'www.php.cn'; $key = 'cn'; if (substr_count($url, $key) == false) echo 'URL中不存在子字符串'.$key; else echo 'URL中存在子字符串'.$key; echo "<br>"; $key1='com'; if (substr_count($url, $key1) == false) echo 'URL中不存在子字符串 '.$key1.' <br>' ; else echo 'URL中存在 '.$key1.' <br>' ; ?>

c.输出:

URL中存在子字符串cn URL中不存在 com

方法二:利用strpos()函数

a.语法格式:

strpos(string,find,start)

  • string 要搜索的字符串。

  • find 要查找的字符串。

  • start 在何处开始搜索。 (可选)

返回值:查找字符串在另一字符串中第一次出现的位置。

ps:strpos() 函数对大小写敏感。

b.示例:

<?php $url = 'www.php.cn'; $key = 'cn'; if (strpos($url, $key) == false) { echo 'URL中不存在子字符串'.$key.' <br>' ; } else { echo 'URL中存在子字符串 '.$key.' <br>' ; } echo "<br>"; $key1 = 'com'; if (strpos($url, $key1) == false) { echo 'URL中不存在子字符串 '.$key1; } else { echo 'URL中存在子字符串 '.$key1 ; } ?>

c.输出:

URL中存在子字符串 cn URL中不存在子字符串 com