Home > Backend Development > PHP Tutorial > PHP – iconv_strpos() function – Find the position of the first occurrence of a substring in a string

PHP – iconv_strpos() function – Find the position of the first occurrence of a substring in a string

WBOY
Release: 2023-08-20 18:46:01
forward
768 people have browsed it

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

In PHP, the iconv_strpos() function is used to read the first character from the given string. It finds the position of the first character in the string. This is a built-in function in PHP.

Syntax

string iconv_strpos(string $haystack, string $needle, int $offset, string $encoding)
Copy after login

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);
?>
Copy after login

Output

int(0)
Copy after login

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);
?>
Copy after login

Output

int(6)
Copy after login

The above is the detailed content of PHP – iconv_strpos() function – Find the position of the first occurrence of a substring in a string. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template