Home >Backend Development >PHP Tutorial >Use PHP to view your own people in the URL

Use PHP to view your own people in the URL

autoload
autoloadOriginal
2021-03-25 10:31:162010browse

Use PHP to view your own people in the URL

Method 1: Use the substr_count() function

a. Grammar format:

substr_count(string,substring,start,length)
  • string: The string being checked.

  • #substring: The string to search for.

  • #start: Specifies where to start searching in the string. (Optional)

  • #length: Specifies the length of the search. (Optional)

Return value: Returns the number of times the substring appears in the string.

b. Example:

<?php 

$url = &#39;http://www.php.cn&#39;; 
$key = &#39;cn&#39;; 
if (substr_count($url, $key) == false) 

  echo &#39;URL中不存在子字符串&#39;.$key; 
 else 
  echo &#39;URL中存在子字符串&#39;.$key; 
  
echo "<br>";

$key1=&#39;com&#39;;
if (substr_count($url, $key1) == false)
 echo &#39;URL中不存在子字符串 &#39;.$key1.&#39; <br>&#39; ; 
 else 
  echo &#39;URL中存在 &#39;.$key1.&#39; <br>&#39; ;

?>

c. Output:

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

Method 2: Use the strpos() function

a. Syntax format:

strpos(string,find,start)
  • string The string to be searched.​

  • #find The string to be found.​

  • #start Where to start the search. (Optional)

Return value: Find the first occurrence of a string in another string.

ps: The strpos() function is case-sensitive.

b.Example:

<?php 

$url = &#39;http://www.php.cn&#39;;  

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

c.Output:

URL中存在子字符串 cn 

URL中不存在子字符串 com

Recommendation:《php video tutorial》《php tutorial

The above is the detailed content of Use PHP to view your own people in the URL. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn