在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.
parametersiconv_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 valuesthe 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() 函数 – 在字符串中查找第一个出现的子串的位置的详细内容。