Open In App

PHP iconv_strpos() Function

Improve
Improve
Like Article
Like
Save
Share
Report

The iconv_strpos() is an inbuilt function in PHP that is used for finding a position in the substring and returning that position.

Syntax:

iconv_strpos( 
    string $haystack, 
    string $needle, 
    int $offset = 0, 
    ?string $encoding = null ): int|false

Parameters: The function takes 4 parameters that are described below:

  • $haystack: This parameter species the complete string.
  • $needle: This parameter specifies the substring to search for.
  • $offset: The position in $haystack to start searching from. It will evaluate from the end of the string if the value of offset is found to be negative.
  • $encoding: This parameter specifies the character set to use. The default value is the internal encoding set used. It transforms & provides the string & implements it as the ordinal value of a character if the haystack or needle is not found in a string.

Return Values: The iconv_strpos() returns the first occurrence of $needle in $haystack otherwise it will return “false” if the $needle is not found in $haystack.

Example 1: The following program demonstrates the iconv_strpos() function.

PHP




<?php   
$str = "Hello world!";
$pos = iconv_strpos($str, 'world');
echo $pos;
?>


Output:

6

Example 2: The following program demonstrates the iconv_strpos() function

PHP




<?php   
$str = "München ist schön.";
$pos = iconv_strpos($str, 'schön', 0, 'UTF-8');
echo $pos;
?>


Output:

12

Reference: https://www.php.net/manual/en/function.iconv-strpos.php


Last Updated : 07 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads