我们已经了解了 strpos()和stripos()的 这有助于找到另一个字符串中第一个出现的字符串。在本文中,我们将了解另外两个类似的函数strrpos()和strrpos()。
null
PHP中的strrpos()
与strpos()不同,strrpos()函数帮助我们找到另一个字符串中最后一个字符串出现的位置。此函数返回与字符串最后一次出现的位置相对应的整数值。这个函数是 区分大小写 ,这意味着它对大小写字符的处理方式不同。
语法:
strrpos(original_str, search_str, start_pos)
参数 在语法中指定的三个参数中,两个是必需的,一个是可选的。这三个参数描述如下:
- 原件(强制性): 这个参数指的是原始字符串,我们需要在其中搜索所需字符串的出现。
- 搜索(强制): 这个参数指的是我们需要搜索的字符串。
- 启动位置(可选): 指字符串的位置,搜索必须从该位置开始。
返回类型 :此函数返回一个整数值,该整数值表示上次进行字符串搜索的原始字符串的索引。
例子:
PHP
<?php // PHP code to search for a specific string's position // last occurrence using strrpos() case-sensitive function function Search( $search , $string ){ $position = strrpos ( $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 19
PHP中的stripos()
与stripos()不同,stripos()函数帮助我们找到另一个字符串中最后出现的字符串的位置。此函数返回与字符串最后一次出现的位置相对应的整数值。这个函数是 不区分大小写 ,这意味着它对大小写字符一视同仁。
语法:
strripos(original_str, search_str, start_pos)
参数 在语法中指定的三个参数中,两个是必需的,一个是可选的。这三个参数描述如下:
- 原件(强制性): 这个参数指的是原始字符串,我们需要在其中搜索所需字符串的出现。
- 搜索(强制): 这个参数指的是我们需要搜索的字符串。
- 启动位置(可选): 指字符串的位置,搜索必须从该位置开始。
返回类型 :此函数返回一个整数值,该整数值表示上次进行字符串搜索的原始字符串的索引。
例子:
PHP
<?php // PHP code to search for a specific string // last occurrence using strripos() case-insensitive function function Search( $search , $string ){ $position = strripos ( $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 19
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END