PHP strpos() and stripos()用法及代码示例

发布时间:2025-09-10 05:04

阅读并理解代码示例,模仿编写 #生活技巧# #数码产品使用技巧# #编程入门指南#

当前位置: 首页>>编程示例 >>用法及示例精选 >>正文

strpos() in PHP

此函数可帮助我们找到一个字符串在另一个字符串中首次出现的位置。这将返回字符串首次出现的位置的整数值。该函数区分大小写,这意味着它对大写和小写字符的处理方式有所不同。

用法:


strpos(original_str, search_str, start_pos)

使用参数
在语法中指定的三个参数中,两个是必需的,一个是可选的。以下是三个参数的说明:

original_str(强制性):此参数表示原始字符串,我们需要在其中搜索所需字符串的出现。 search_str(强制性):此参数表示我们需要搜索的字符串。 start_pos(可选):指从必须开始搜索的字符串位置。

返回类型:此函数返回一个整数值,该整数值表示字符串search_str首次出现的original_str的索引。

例:

<?php    // PHP code to search for a specific string's position // first occurrence using strpos() case-sensitive function function Search($search, $string){     $position = strpos($string, $search, 5);       if ($position == true){         return "Found at position:" . $position;     }     else{         return "Not Found";     } }    // Driver Code $string = "Welcome to GeeksforGeeks"; $search = "Geeks"; echo Search($search, $string); ?>

输出:

Found at position 11

stripos() in PHP

此函数还可以帮助我们找到一个字符串在另一个字符串中首次出现的位置。这将返回字符串首次出现的位置的整数值。此函数不区分大小写,这意味着它会同等地对待大写和小写字符。此函数与strpos()相似,不同之处在于它不区分大小写,而strpos()区分大小写。

用法:

stripos(original_str, search_str, start_pos)

使用参数
在语法中指定的三个参数中,两个是必需的,一个是可选的

original_str(强制性):此参数表示原始字符串,我们需要在其中搜索所需字符串的出现。 search_str(强制性):此参数表示我们需要找到的字符串。 start_pos(可选):此参数指的是必须从搜索开始的字符串位置。

返回类型:此函数返回一个整数值,该整数值表示字符串search_str首次出现的original_str的索引。

例:

<?php // PHP code to search for a specific string // first occurrence using stripos() case-insensitive function function Search($search, $string){     $position = stripos($string, $search, 5);       if ($position == true){         return "Found at posiion " . $position;     }     else{         return "Not Found";     } }    // Driver Code $string = "Welcome to GeeksforGeeks"; $search = "geeks"; echo Search($search, $string); ?>

输出:

Found at position 11

相关用法

PHP startsWith() and endsWith()用法及代码示例 PHP strrpos() and strripos()用法及代码示例

注:本文由纯净天空筛选整理自Chinmoy Lenka大神的英文原创作品 PHP | strpos() and stripos() Functions。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。

网址:PHP strpos() and stripos()用法及代码示例 https://klqsh.com/news/view/231331

相关内容

PHP strpos() 函数
深入理解PHP中的strpos函数(php判断字符串strpos)
PHP中使用strpos函数高效检测字符串中是否包含特定字符的方法详解
PHP strpos 字符串函数
php禁止跨域调用api(来自文心快码)
使用php开发圈子系统特点,如何获取圈子系统源码,社交圈子运营以及圈子系统的功能特点,圈子系统,允许二开,免费源码,APP 小程序 H5
影视电视剧电影资讯自适应源码
Peak码支付V2.0版本
详谈HTML中script标签(附代码)
标准化层(BN,LN,IN,GN)介绍及代码实现

随便看看