strchr()函数是PHP中的内置函数,用于在另一个字符串(例如originalStr)中搜索给定字符串的第一个匹配项(例如searchStr),并从首次出现的searchStr中返回originalStr的其余字符串orignalStr。
注意:strchr()函数区分大小写。
用法:
strchr($originalStr, $searchStr, $before_search
参数:
- $originalStr:此参数指定要在其中搜索单词的字符串。这是强制性的
 - $searchStr:它在给定的$originalStr中指定要搜索的单词,也可以是字符或数字,如果传递了数字,则在$originalStr中搜索等效的ASCII值字符。这是强制性的。
 - $before_search: 这是一个可选参数,当设置为True时,它会在$searchStr首次出现之前返回$originalStr的一部分。默认情况下将其设置为false。
 
返回值:它根据以下三种情况返回一个字符串:
- 它返回的字符串从找到$searchStr时第一次出现在$originalStr中的$searchStr开始到$originalStr的末尾。
 - 当给定的$originalStr中不存在$searchStr时,它不返回任何内容。
 - 当$before_search设置为TRUE时,它将返回第一次出现$searchStr之前的字符串部分。
 
例子:
Input : $originalStr = "geeks for geeks" 
        $searchStr = "geeks" 
Output : geeks for geeks 
Input : $originalStr = "geeks for geeks" 
        $searchStr = "for" 
Output : for geeks 
Input : $originalStr = "striver has published 180 articles"
        $searchStr = "has"    $before_search = TRUE
Output :  striver
 
Input: $originalStr = "geeks for geeks" $searchStr = "gfg" 
Output: No output 
以下示例程序旨在说明PHP中的strchr()函数:
程序1:发现单词时演示strchr()函数的程序。
<?php 
// Program to demonstrate the chr()  
// function when word is found  
$originalStr = "geeks for geeks";  
$searchStr = "geeks" ; 
  
// prints the string from the  
// first occurrence of the $searchStr 
echo strchr($originalStr, $searchStr); 
?>输出:
geeks for geeks
程序2:找不到单词时演示strchr()函数的程序。
<?php 
// Program to demonstrate the chr()  
// function when word is not found  
$originalStr = "geeks for geeks";  
$searchStr = "gfg" ; 
  
// prints the string from the  
// first occurrence of the $searchStr 
echo strchr($originalStr, $searchStr); 
?>输出:
No Output
程序3:当找到单词并将$before_search设置为true时,演示strchr()函数的程序。
<?php 
// Program to demonstrate the chr()  
// function when word is found and 
// $before_search is set to true  
$originalStr = "geeks for geeks";  
$searchStr = "for" ; 
  
// prints the string from the  
// first occurrence of the word 
echo strchr($originalStr, $searchStr, true); 
?>输出:
geeks
程序4:传递并找到部分单词时演示strchr()函数的程序。
<?php 
// Program to demonstrate the chr()  
// function when a part of word is passed and found 
$originalStr = "geeks for geeks";  
$searchStr = "eks" ; 
  
// prints the string from the 
// first occurrence of the word 
echo strchr($originalStr, $searchStr); 
?>输出:
eks for geeks
程序5:传递数字并搜索其等效ASCII字符时演示strchr()函数的程序。
<?php 
// Program to demonstrate the chr()  
// function when a number is passed and its equivalent 
// ASCII character is searched 
  
$originalStr = "geeks for geeks";  
  
// 101 is the ASCII value of e  
$searchStr = 101 ; 
  
echo strchr($originalStr, $searchStr); 
?>输出:
eeks for geeks
参考:
http://php.net/manual/en/function.strchr.php
相关用法
- d3.js d3.map.get()用法及代码示例
 - PHP Ds\Map get()用法及代码示例
 - p5.js max()用法及代码示例
 - CSS var()用法及代码示例
 - PHP next()用法及代码示例
 - d3.js d3.map.has()用法及代码示例
 - p5.js sq()用法及代码示例
 - PHP each()用法及代码示例
 - p5.js str()用法及代码示例
 - d3.js d3.set.has()用法及代码示例
 - PHP Ds\Map put()用法及代码示例
 - PHP each()用法及代码示例
 
注:本文由纯净天空筛选整理自Striver大神的英文原创作品 PHP | strchr() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
