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 = 'http://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. 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 = 'http://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.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!