首页 > 后端开发 > php教程 > PHP – iconv_strpos() 函数 – 在字符串中查找第一个出现的子串的位置

PHP – iconv_strpos() 函数 – 在字符串中查找第一个出现的子串的位置

WBOY
发布: 2023-08-20 18:46:01
转载
768 人浏览过

PHP – iconv_strpos() 函数 – 在字符串中查找第一个出现的子串的位置

在PHP中,iconv_strpos()函数用于从给定的字符串中读取第一个字符。它找到字符串中第一个字符的位置。这是PHP中的内置函数。

语法

string iconv_strpos(string $haystack, string $needle, int $offset, string $encoding)
登录后复制

Note: strpos(), the return value of iconv_strpos() is the number of characters that appear before the needle, rather than the offset in bytes to the position where the needle has been found. The characters are counted based on the specified character set encoding.

Parameters

iconv_strpos() function accepts four different parameters− $haystack, $needle, $offset and $encoding.

  • $haystack− It denotes the whole string.

  • $needle− The $needle parameter is used to search the substring from the given whole string.

  • $offset− The $offset parameter is optional it is used to specify the position from which the search should be performed. If the offset is negative then it will count from the end of the string.

  • $encoding− if the $encoding parameter is absent or null, then the string will assume that it may be encoded in iconv.internal_encoding.

Return Values

The iconv_strpos() function returns the numeric position of the first occurrence of the needle in the haystack. If the needle is not found, then the function will return False.

Note: From PHP 8.0 version, encoding is nullable and from PHP 7.1, iconv_strpos() function support for the negative offsets has been added.

Example 1

 Live Demo

<?php
   # UTF-8 string
   $int = iconv_strpos("hello world!", "hello",0, "UTF-8");
   // It will returns the number of character
   var_dump($int);
?>
登录后复制

输出

int(0)
登录后复制

Example 2

 Live Demo

<?php
   # UTF-8 string
   $int = iconv_strpos("hello world!", "world",0, "UTF-8");

   // It will returns the number of character
   var_dump($int);
?>
登录后复制

输出

int(6)
登录后复制

以上是PHP – iconv_strpos() 函数 – 在字符串中查找第一个出现的子串的位置的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:tutorialspoint.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板