Open In App

How to get the position of character in a string in PHP ?

Last Updated : 07 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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
  
// Consider the string
$str1 = "Hello geeks for geeks";
  
// Get the position of 'f' character
echo strpos($str1, 'f');
echo "\n";
  
// Get the position of 'H' character
echo strpos($str1, 'H');
echo "\n";
  
// Get the position of 'l' character
echo strpos($str1, 'l');
echo "\n";
  
// Get the position of ' ' space  
echo strpos($str1, ' ');
  
?>


Output

12
0
2
5

Example 2:

PHP




<?php
  
// Consider the string
$str1 = "Hello geeks for geeks";
  
// Get the position of 'f' character
// starts from 5th index
echo strpos($str1, 'f', 5);
echo "\n";
  
// Get the position of 'H' character
// starts from 8th index
// No output since H is present at 1st
// position, we started from 8th position 
echo strpos($str1, 'H', 8);
  
?>


Output

12


Similar Reads

How to Get Character of Specific Position using JavaScript ?
Get the Character of a Specific Position Using JavaScript We have different approaches, In this article we are going to learn how to Get the Character of a Specific Position using JavaScript Below are the methods to get the character at a specific position using JavaScript: Table of Content Method 1: Using String charAt() MethodMethod 2: Using Stri
3 min read
How to get the last character of a string in PHP ?
In this article, we will find the last character of a string in PHP. The last character can be found using the following methods. Using array() methodUsing substr() method Using array() Method: In this method, we will find the length of the string, then print the value of (length-1). For example, if the string is "Akshit" Its length is 6, in zero-i
2 min read
What is the difference between position:sticky and position:fixed in CSS ?
In CSS, position: sticky and position: fixed both offer elements the ability to stay in a fixed position during scrolling. These two CSS properties, while seemingly similar, have distinct behaviors that can significantly impact the layout and user experience of a webpage. This article will tell you the differences, providing you more understanding
6 min read
Insert string at specified position in PHP
Given a sentence, a string and the position, the task is to insert the given string at the specified position. We will start counting the position form zero. See the examples below. Input : sentence = 'I am happy today.' string = 'very' position = 4 Output :I amvery happy today. Begin counting with 0. Start counting from the very first character ti
1 min read
How to Insert Characters in a String at a Certain Position in PHP ?
In this article, you will learn how to insert characters into a string at a specific position in PHP. Given a string str and an integer position that describes the index in the original string where the desired string insertStr will be added. This can be achieved using various approaches, such as string concatenation, and the substr_replace() funct
2 min read
How to get the last character of a string in JavaScript ?
In this article, we are going to learn how can we get the last character of a string in JavaScript. we have given a string of size len, the task is to get the last character of a string using JavaScript. There are several methods to solve this problem: Table of Content Using charAt() MethodUsing str.slice() MethodUsing str.substr() MethodUsing Java
3 min read
JavaScript Program to Get a Non-Repeating Character From the Given String
In JavaScript, we can find the non-repeating character from the input string by identifying the characters that occur only once in the string. There are several approaches in JavaScript to get a non-repeating character from the given string which are as follows: Table of Content Using indexOf and lastIndexOf methodsUsing Set and Array.from()Using M
3 min read
How to Get Character Array from String in JavaScript ?
The string is defined as a sequence of characters. In this article, we will learn how to get a character array from a string in JavaScript. Table of Content JavaScript String split() FunctionJavaScript Array.from() FunctionJavaScript Spread OperatorUsing for ... of LoopUsing Array.prototype.map()JavaScript String split() FunctionThe str.split() fun
3 min read
JavaScript Insert a string at position X of another string
Given two strings, the task is to insert one string in another at a specified position using JavaScript. We're going to discuss a few methods, these are: Methods to Insert String at a Certain IndexTable of Content Method 1: Using JavaScript String slice() and Array join() MethodsMethod 2: JavaScript String substr() Method Method 3: Using JavaScript
3 min read
How to get position of an element relative to the document or parent using jQuery ?
In order to get the location of an element relative to the document, jQuery offset() method is used. The offset() method is an inbuilt method in jQuery which is used to set or returns the offset coordinates of the selected element. We can also use the jQuery position() method. The position() method is an inbuilt method in jQuery which is used to fi
2 min read