0% found this document useful (0 votes)
94 views

PHP Instr

This document provides an overview of the strpos function in PHP, which finds the position of the first occurrence of a substring within a larger string. Strpos has the same functionality as the instr function used in other languages like ASP and VB, but with a different name. The strpos syntax takes three parameters - the main string to search, the substring to find, and an optional starting position. It returns the integer position of the matched substring or FALSE if no match is found. Examples are given to demonstrate finding substring positions both with and without specifying a start position. Other related string functions are also listed.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views

PHP Instr

This document provides an overview of the strpos function in PHP, which finds the position of the first occurrence of a substring within a larger string. Strpos has the same functionality as the instr function used in other languages like ASP and VB, but with a different name. The strpos syntax takes three parameters - the main string to search, the substring to find, and an optional starting position. It returns the integer position of the matched substring or FALSE if no match is found. Examples are given to demonstrate finding substring positions both with and without specifying a start position. Other related string functions are also listed.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

PHP instr() tutorial

An article from Learn PHP in 24 Hours

instr is a function used to find the first occurrence of a sub-string within a larger string.

Unlike ASP or VB, PHP does not use the terminology instr, but instead uses strpos.
Although named differently, strpos has the same basic functionality as instr.

strpos syntax:

int strpos (string $string1, mixed $string2 [, int $start = 0])

string1

String to search in

string2

The sub-string to find within string1

start

Optional. This is the character position to start at. This defaults to the first character if
omitted (start = 0 by default).

Return value:

Returns the integer position of the first character withing string1. FALSE is returned if
string2 is not found within string1.

What about some examples then?


$position = strpos("this is my example", "is");

will return the integer value 2.

Whereas,

$position = strpos("this is my example", "is", 3);

will return the integer value 5.


Other related functions you might enjoy!

strrpos() - Find position of last occurrence of a char in a string


stripos() - Find position of first occurrence of a case-insensitive string
strripos() – Find position of last occurrence of a case-insensitive string in a string
strrchr() - Find the last occurrence of a character in a string
substr() - Return part of a string
stristr() - Case-insensitive strstr
strstr() - Find first occurrence of a string

(function list from php.net)

You might also like