按照官网的解释是:strchr()函数在PHP内置函数。本篇文章是由sky8g网站原作,禁止抄袭。
用于搜索给定的字符串(比如第一次出现的字符串searchstr)在另一个字符串(比如originalStr),并从返回字符串的其余部分originalStr从第一个开始在orignalStr中出现searchStr。
注意:strchr函数区分大小写。
语法:
1 2 | strchr($originalStr, $searchStr, $before_search) |
其中包括3个参数:
- $originalStr: (必选项)。给定的源字符串。
- $originalStr: (必选项)。给定的要搜索的字符串
- $originalStr: (可选)。默认值为 false ,当设置为true 时,在第一次出现$searchStr之前返回$originalStr的一部分
返回值:有三种情况
- 当找到$searchStr时,它返回值是一段的字符串,从$ originalStr字符串中搜索$searchStr字符串的第一次出现的字符串开始到$originalStr的末尾的字符串。
- 当$searchStr字符串在给定的$originalStr字符串中不存在时,它不返回任何内容。
- 当$before_search设置为TRUE 时,它返回值是在$searchStr第一次出现之前字符串的一部分。
例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | Input : $originalStr = "sky8g for sky8g" $searchStr = "sky8g" Output : sky8g for sky8g Input : $originalStr = "sky8g for sky8g" $searchStr = "for" Output : for sky8g Input : $originalStr = "striver has published 180 articles" $searchStr = "has" $before_search = TRUE Output : striver Input: $originalStr = "sky8g for sky8g" $searchStr = "g3fg" Output: No output |
下面详细介绍:strchr()函数
程序1:查找到单词时strchr()函数的程序。
1 2 3 4 5 6 7 8 9 10 | <?php // Program to demonstrate the chr() // function when word is found $originalStr = "sky8g for sky8g"; $searchStr = "sky8g" ; // prints the string from the // first occurence of the $searchStr echo strchr($originalStr, $searchStr); ?> |
输出
1 | sky8g for sky8g |
程序2:在找不到单词的strchr()函数的程序
1 2 3 4 5 6 7 8 9 10 | <?php // Program to demonstrate the chr() // function when word is not found $originalStr = "sky8g for sky8g"; $searchStr = "gfg" ; // prints the string from the // first occurence of the $searchStr echo strchr($originalStr, $searchStr); ?> |
输出
1 |
程序3:当找到字符串时并且$before_search设置为true时,strchr()函数的程序。
1 2 3 4 5 6 7 8 9 10 11 | <?php // Program to demonstrate the chr() // function when word is found and // $before_search is set to true $originalStr = "sky8g for sky8g"; $searchStr = "for" ; // prints the string from the // first occurence of the word echo strchr($originalStr, $searchStr, true); ?> |
输出
1 | sky8g |
程序4:当查找到字符串一部分时,strchr()函数的程序。
1 2 3 4 5 6 7 8 9 10 | <?php // Program to demonstrate the chr() // function when a part of word is passed and found $originalStr = "sky8g for sky8g"; $searchStr = "8" ; // prints the string from the // first occurence of the word echo strchr($originalStr, $searchStr); ?> |
输出
1 | 8g for sky8g |
程序5:在传递数字并搜索其等效ASCII字符时演示strchr()函数的程序。
1 2 3 4 5 6 7 8 9 10 11 12 | <?php // Program to demonstrate the chr() // function when a number is passed and its equivalent // ASCII character is searched $originalStr = "sKy8g for sky8g"; // 107 is the ASCII value of k $searchStr = 107 ; echo strchr($originalStr, $searchStr); ?> |
输出
1 | ky8g |
本篇文章参考:
http://php.net/manual/en/function.strchr.php
希望对你有帮助,有不懂得地方请留言!