In this article, we will get the position of the character in the given string in PHP. String is a set of characters. We will get the position of the character in a string by using strpos() function.
Syntax:
strpos(string, character, start_pos)
Parameters:
- string (mandatory): This parameter refers to the original string in which we need to search the occurrence of the required string.
- character (mandatory): This parameter refers to the string that we need to search.
- start_pos (optional): Refers to the position of the string from where the search must begin.
Return Value: It returns the index position of the character.
Example 1: PHP Program to get the position of particular character in the given string.
PHP
<?php
$str1 = "Hello geeks for geeks" ;
echo strpos ( $str1 , 'f' );
echo "\n" ;
echo strpos ( $str1 , 'H' );
echo "\n" ;
echo strpos ( $str1 , 'l' );
echo "\n" ;
echo strpos ( $str1 , ' ' );
?>
|
Example 2:
PHP
<?php
$str1 = "Hello geeks for geeks" ;
echo strpos ( $str1 , 'f' , 5);
echo "\n" ;
echo strpos ( $str1 , 'H' , 8);
?>
|
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
07 Oct, 2021
Like Article
Save Article