PHP strpos
The following article provides an outline for PHP strpos. There is a large set of built-in functions to make string related operations easier for the coder or the developer. The strops() is one of the popular string functions we can use as per requirements as its name suggests for dealing with the position in any string in PHP. Using this function, we can get the position of the string inside another string. This function can be used in various ways with various scenarios like we can get the first occurrence position of a string inside another. We can get the last occurrence position of the string inside another string. This function also can be used to search one string into another string.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax of PHP strpos
Since this can be used in various ways, we can see the various syntax:
strpos($string, $toBeSearched)
Parameter Description:
- $string: This is a string upon which the search needs to be done.
- $toBeSearched: This is a string that needs to search inside the above string.
Return Type:
It returns the numeric value if the search finds it; otherwise it gives the false.
stripos($string, $toBeSearched, $startPostion)
We can see one additional parameter apart from the earlier one. $startPostion is nothing, but it’s all about where the search will be started.
strrpos($string, $toBeSearched, $startPostion)
This function can be used to search string into another string. This start was searching in the reverse order. The word ‘r’ in the middle of this function does the trick of searching the string into another string in the reverse order.
How does strpos() Function work in PHP?
- To use the strpos() function and other related functions to it, we need to have two strings, one string will be searched into another string.
- We have various other built-in functions like stripos(), strrpos(), strripos() etc. stripos() can be used to search the string of a case-insensitive search string. Strrpos() can be used to search the string in reverse order.
- The strripos() can be used to search the sting in the reverse order of a case-insensitive search string.
Examples of PHP strpos()
Given below are the examples mentioned :
Example #1
A basic program using strops() function with only two parameters, string one and the search string.
Code:
<?php $string = "I love php, I love coding in php too!"; $searchString = "php"; $output = strpos($string, $searchString); echo "String:. “ $string; echo "<br>Output: ". $output; ?>
Output:

In the above example, we can see that in a string we are trying to search a PHP string, the use of strops() for searching the PHP word gives the output 7. 7 is the index of the first occurrence of the PHP word. The strops() is a case-sensitive function to find any string inside another string.
Example #2
Let’s try to replicate the same as in example 1 but with the PHP’s different case (upper or lower).
Code:
<?php $string = "I love php, I love coding in php too!"; $searchString = "PHP"; $output = strpos($string, $searchString); // this will give the false result echo "String: ". $string; echo "<br>Output: ". $output; $output1 = stripos($string, $searchString); // this will give the the 7 as a result echo "<br>String: ". $string; echo "<br>Output: ". $output1; ?>
Output:

In the above example, we can see that if the case if not matching in this case the strops() will not be doing the job, for this, we have stripos() with the same syntax and the parameter for doing the same job.
Example #3
Let’s see an example of using the third parameter as well with the strops().
Code:
<?php $string = "I love php, I love coding in php too!"; $searchString = "PHP"; $output = strpos($string, $searchString,8); // this will give the false result echo "String: ". $string; echo "<br>Output: ". $output; $output1 = stripos($string, $searchString,8); // this will give the the 7 as a result echo "<br>String: ". $string; echo "<br>Output: ". $output1; ?>
Output:

Just a little different from the previous example gives a huge difference as we can see in the result. We have put the 8 as a third parameter. Using 8 as a third parameter means the search will begin from position 8, not from position 0.
Example #4
Let’s see a bit of a professional example (handling types of return).
Code:
<?php
$string = "https://www.educba.com/tutorials/?source=menu";
$searchString = "tutorials";
$output = strpos($string, $searchString); // this will give the false result
if($output==false){
echo "Search not found.";
}else{
echo "Search found.";
}
?>Output:

Example #5
strrpos() can be used to search the position of the string into another string in the reverse order.
Code:
<?php
$string = "I love PHP; I like to code in PHP";
$searchString = "PHP";
$output = strrpos($string, $searchString); // this will give the false result
if($output==false){
echo "Search not found.";
}else{
echo "Search found: ".$output;
}
?>Output:

The same way, we can search the string into another of case-insensitive search string using the strripos() function. Now, we have seen the example of all the types of strops() we can use per the business requirements.
Conclusion
In PHP programming language, we can use the strpose() and the stripos() built-in functions to search a string into another. The strops() is a case-sensitive function to deal with the searching, and in PHP stripos() can be used for case-insensitive to do the same job like strops(). The stripos() don’t check for the same case. The developer must be careful enough while using these functions because if the searches match, it gives the numeric as a result but in case no search is found, it returns false.
The above is the detailed content of PHP strpos. For more information, please follow other related articles on the PHP Chinese website!
Hot AI Tools
Undresser.AI Undress
AI-powered app for creating realistic nude photos
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undress AI Tool
Undress images for free
Clothoff.io
AI clothes remover
AI Hentai Generator
Generate AI Hentai for free.
Hot Article
Hot Tools
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
1377
52
PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian
Dec 24, 2024 pm 04:42 PM
PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati
Discuss CakePHP
Sep 10, 2024 pm 05:28 PM
CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu
CakePHP File upload
Sep 10, 2024 pm 05:27 PM
To work on file upload we are going to use the form helper. Here, is an example for file upload.
How To Set Up Visual Studio Code (VS Code) for PHP Development
Dec 20, 2024 am 11:31 AM
Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c
How do you parse and process HTML/XML in PHP?
Feb 07, 2025 am 11:57 AM
This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an
CakePHP Quick Guide
Sep 10, 2024 pm 05:27 PM
CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.
PHP Program to Count Vowels in a String
Feb 07, 2025 pm 12:12 PM
A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total
Explain JSON Web Tokens (JWT) and their use case in PHP APIs.
Apr 05, 2025 am 12:04 AM
JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,


