Categories: PHP

strpos – PHP String Functions

Syntax :

strpos ( string, search_string, start );

Description :

It’s an inbuilt function of PHP. strpos() function  Find the position of the first occurrence of a search_string in a string.

Note: The function is case-sensitive.
Note: This function is binary-safe.

Parameter :

[table caption=”” width=”100%” colwidth=”15%|15%|15%|55%” colalign=”left|left|left|left”]
Name, Required /Optional, Value Type, Description

string, Required, String, Main string to check into.

search_string, Required, String, String to be searched for.

start, OptionalString,  Specifies where to start searching in string.

[/table]


Output :

Returns the of the first occurrence of a search_string inside string. Also note that string positions start at 0, and not 1.

Returns FALSE if the search_string was not found.


ChangeLog :

[table caption=”” width=”100%” colwidth=”25%|75%” colalign=”left|left”]
PHP Version, Description
7.1.0, Support for negative length has been added.

[/table]


Related articles : stripos(), strrpos(), strripos(), strstr(), substr(), strpbrk().


strpos() – PHP Functions Example 1 :
<?php 
$mystring = 'tutor';
$findme = 't';
$pos = strpos($mystring, $findme);

// Note our use of ===. Simply == would not work as expected
// because the position of 't' was the 0th (first) character.
if ($pos === false) {
echo "The string '$findme' was not found in the string '$mystring'";
} else {
echo "The string '$findme' was found in the string '$mystring'";
echo " and exists at position $pos";
}
?>

Output of above code in the browser is as below:

The string ‘t’ was found in the string ‘tutor’ and exists at position 0

strpos() – PHP Functions Example 2 : Using length parameter.
<?php
// We can search for the character, ignoring anything before the offset
$newstring = 'hello from tutor. hi all.';
$pos = strpos($newstring, 'h', 1); 
echo $pos;
?>

Output of above code in the browser is as below:

18

strpos() – PHP Functions Example 3 :   Returns FALSE if the search_string was not found.
<?php
// We can search for the character, ignoring anything before the offset
$newstring = 'hello from tutor. hi all.';
$pos = strpos($newstring, 'b', 1); 
var_dump($pos);
?>

Output of above code is :

bool(false)

jyoti rani

Recent Posts

Exploring the Best Toy Store in Noida: A Paradise for Kids and Collectors

Toys have always played a crucial role in childhood, fueling imagination, creativity, and learning. Whether you're looking for educational playsets,…

16 hours ago

A Unique Toy Shopping Experience: The Best Toy Store in Delhi and Across India

Toys are an essential part of childhood, shaping creativity, learning, and fun-filled memories. Whether it’s the soft embrace of plush…

2 days ago

A World of Play: The Best Toy Shopping Experience in Noida

Toys are a gateway to imagination, creativity, and learning. Whether it’s a child’s first plush toy, a set of engaging…

3 days ago

The Ultimate Guide to Finding the Best Toy Shops in Gurgaon

Toys are more than just playthings; they help children explore, learn, and develop essential life skills. From interactive sensory toys…

4 days ago

The Ultimate Toy Shopping Guide: Finding the Best Toy Store in Noida

Toys are more than just playthings; they are tools that foster creativity, motor skills, and cognitive development in children. From…

5 days ago

Exploring the Best Toy Shop in Delhi: A Blend of Fun, Learning, and Creativity

Toys play an integral role in a child’s growth and development. They spark creativity, encourage learning, and provide endless hours…

1 week ago