★ wanayoo — archive 1999 http://www.php.net/manual/function.strpos.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to Strings
Quick Reference
German version of this pageJapanese version of this pageItalian version of this pageFrench version of this pageHungarian version of this page
Strings
* AddCSlashes
* AddSlashes
* bin2hex
* Chop
* Chr
* chunk_split
* convert_cyr_string
* count_chars
* crc32
* crypt
* echo
* explode
* flush
* get_html_translation_table
* get_meta_tags
* hebrev
* hebrevc
* htmlentities
* htmlspecialchars
* implode
* join
* levenshtein
* ltrim
* md5
* Metaphone
* nl2br
* ob_start
* ob_get_contents
* ob_end_flush
* ob_end_clean
* ob_implicit_flush
* Ord
* parse_str
* print
* printf
* quoted_printable_decode
* quotemeta
* rtrim
* sscanf
* setlocale
* similar_text
* soundex
* sprintf
* strcasecmp
* strchr
* strcmp
* strcspn
* strip_tags
* stripcslashes
* stripslashes
* stristr
* strlen
* str_pad
* strpos
* strrchr
* str_repeat
* strrev
* strrpos
* strspn
* strstr
* strtok
* strtolower
* strtoupper
* str_replace
* strtr
* substr
* substr_count
* substr_replace
* trim
* ucfirst
* ucwords
Manual: strpos
View the source code for this pageSearch the site



Previous page
 str_pad
 Updated
Sat, 12 Aug 2000
strrchr 
Next page


strpos

(PHP3 , PHP4 )

strpos --  Find position of first occurrence of a string

Description

int strpos (string haystack, string needle [, int offset])

Returns the numeric position of the first occurrence of needle in the haystack string. Unlike the strrpos(), this function can take a full string as the needle parameter and the entire string will be used.

If needle is not found, returns false.

Note: It is easy to mistake the return values for "character found at position 0" and "character not found". Here's how to detect the difference:


// in PHP 4.0b3 and newer:
$pos = strpos ($mystring, "b");
if ($pos === false) { // note: three equal signs
    // not found...
}

// in versions older than 4.0b3:
$pos = strpos ($mystring, "b");
if (is_string ($pos) && !$pos) {
    // not found...
}
	

If needle is not a string, it is converted to an integer and applied as the ordinal value of a character.

The optional offset parameter allows you to specify which character in haystack to start searching. The position returned is still relative to the the beginning of haystack.

See also strrpos(), strrchr(), substr(), stristr(), and strstr().


User Contributed Notes: strpos


sbedberg@ucdavis.edu
15-Dec-1998 09:46
Note that strpos' character index is zero-based; a character match on the first character returns zero (strpos('ab', 'a') = 0). Thus, a test such as
<PRE>
if (strpos($hay, $needle)) { ... }
</PRE>
can give a false negative. You can use the empty() function or an explicit test (strpos(...) >= 0) to avoid this.



jgreen@netshel.net
30-Dec-1998 04:16
The only way I found to test for a string match on the beginning of "haystack" is to use:
<pre>if (is_int(strpos($haystack, $needle))) { }..</pre>
strpos returns an empty string "" if no match is found, an integer otherwise. Therefore tests like empty(strpos($haystack, $needle)) and strpos($haystack, $needle) >= 0 don't work, because empty strings "" have a value and are evaluated as 0.



ken@kyler.com
30-Apr-1999 11:15
I resolved the problem by adding an underscore "_" to the variable and then testing for "needle". Of course, if you're looking for an underscore, you've got a problem!

<pre>
$newhaystack = "_".$haystack;
if (strpos($newhaystack, $needle)) { ... }</pre>



ligu@hali.elte.hu
30-Jun-1999 11:04
If man simply wants to know wether $substring is a part of $string or not, it's easier to use regular expressions.

<PRE>
ereg($substring,$string);
</PRE>
Returns true if $substring is found anywhere in $string. Note, "ereg" is case sensitive, if You want case insensitive serch, use "eregi" instead.



kirsty@loquax.com
30-Jul-1999 07:40
But using ereg will be a lot slower I think... using a sledgehammer to crack open a nutshell often is :-)


thorsten@arch.usyd.edu.au
26-Aug-1999 08:50
This works for testing for the
first character:
<pre>
if (strpos ($file, (string)$item) == "0")
</pre>

because false (not found) will be converted to ""



sRp@portico.org
27-Aug-1999 09:59
I've always detected the difference between match at 0 and no match this way:

<pre>
if(strlen(strpos($hay,$needle))) { .. }
</pre>

i thought that i had tried the " == '0'" thing before and didn't get it to work in every case, but maby i was mistaken, idk.



brian@digitalco.com
04-Sep-1999 01:31
Its worth mentioning that
if (strpos($hay,$needle)>="0") { ... } works just fine too. Since == just tests for the first character and its better to use
if (substr($hay,1,0)==$needle) { ... } when just checking for the first character since it will be faster to just compare the first character rather than search the whole substring.




h.raaf@i-k-c.net
06-Sep-1999 11:03
If you are testing for the first character you can use something like <pre>if($string[0]==".")</pre>


styx@xlarge.at
07-Dec-1999 09:23
ahrg.. case-sensitive....

walkaround:
strpos("hello world","Hello");
will not work; instead
strpos(strtolower("hello world"), strtolower("Hello"));
will produce the 'right' result...



h.ebelt@addcom.de
15-Mar-2000 08:02
It should be mentioned that u have to use

<pre>
if(strpos($haystack, $needle) >= "0") {...}
</pre>

NOT

<pre>
if(strpos($haystack, $needle) >= 0) {...}
</pre>

The second code would be always true!



jkelle00@yahoo.com
21-Mar-2000 06:10
The function strps() should be re-done in the next release of PHP. So the function would return -1 instead of returning false for not found; because you will never have a negative position.


GrandLibrarian@iname.com
27-Apr-2000 09:02
To find the position of a string in a case-insensitive manner without converting everything to lowercase, try using stristr:

$temp = stristr($haystack,$needle);

$pos = strlen($haystack)-strlen($temp);



zpopi@caramail.com
18-Jul-2000 05:43
strpos *seems* not to work correctly with php4.0. When the needle isn't found, it doesn't return false as it used to do with 3.*.
Making an echo of the return value displays nothing.
So I tried to change the test to :
<PRE>
$pos = strpos($haystack, $needle)
if ( ($pos == "") || ($pos == false) )
{....}
</PRE>

but it doesn't work...
Is it a bug or have i done an unforgivable mistake?
thanks, silvin



dave@arthistory.cc
12-Aug-2000 10:15
$pos = strpos ($mystring, "b");
if ($pos === false) {
//Noot found
}
Should work this way too...
$pos = strpos ($mystring, "b");
if ($pos < 0) {
//Noot found
}



 About Notes


Previous page
 str_pad
 Updated
Sat, 12 Aug 2000
strrchr 
Next page





Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.