Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



// ob_start(); ?> PHP : Function Reference : String Functions : strpos

strpos

Find position of first occurrence of a string (PHP 4, PHP 5)
int strpos ( string haystack, mixed needle [, int offset] )

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

Parameters

haystack
needle

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

offset

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

Return Values

Returns the position as an integer. If needle is not found, strpos() will return boolean FALSE.

Warning:

This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or "". Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

Examples

Example 2461. strpos() examples

<?php
$mystring
= 'abc';
$findme   = 'a';
$pos = strpos($mystring, $findme);

// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' 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";
}

// We can search for the character, ignoring anything before the offset
$newstring = 'abcdef abcdef';
$pos = strpos($newstring, 'a', 1); // $pos = 7, not 0
?>


Notes

Note:

This function is binary-safe.

// $html .= ob_get_clean(); // $doc = new DOMDocument(); // @$doc->loadHTML($html); // $ap=$doc->getElementsByTagName("funcPurpose"); // if($ap){ // // echo "dddddddddddd"; // // // // // $domtxt = new DOMDocument('1.0'); // $domtxt->formatOutput = true; // // // // $elaspan= $doc->getElementsByTagName("span"); // $cc= $elaspan->length; // if($cc>0) // { // for($i=0;$iitem($i)->getAttribute("id") && $elaspan->item($i)->getAttribute("id")=="navTop") // { // $menu= $elaspan->item($i); // } // // } // $dom_sxe = $domtxt->importNode($menu, true); // $dom_sxe = $domtxt->appendChild($dom_sxe); // } // // $ela= $doc->getElementsByTagName("div"); // $cc= $ela->length; // if($cc>0) // { // for($i=0;$iitem($i)->getAttribute("id") && $ela->item($i)->getAttribute("id")=="pageHeaders") // { // $h= $ela->item($i); // // } // // } // // $dom_h = $domtxt->importNode($h, true); // $dom_h = $domtxt->appendChild($dom_h); // } // echo $_mytxt=$domtxt->saveHTML()."
"; // }else{ // echo $_mytxt=$html; // } ?>

Related Examples ( Source code ) » strpos






Code Examples / Notes » strpos

amy_w

You can use this function to find ANY occurence of a string in an array - no matter if it is just part of one of the array elements.
it returns the key of the first found occurence or false
<?php
function search_array($needle,$haystacks) {
$found=false;
foreach ($haystacks as $key => $haystack) {
if (!(strpos($haystack,$needle)===false)) {
$found=$key;
break;
}
}
return ($found);
}
?>


chasesan

You can use strpos to produce a funciton that will find the nth instance of a certain string within a string. Personally I find this function almost more useful then strpos itself.
I kinda wish they would put it stock into php but I doupt thats gonna happen any time soon. ^_^
Here is da code:
<?php
//just like strpos, but it returns the position of the nth instance of the needle (yay!)
function strpos2($haystack, $needle, $nth = 1)
{
//Fixes a null return if the position is at the beginning of input
//It also changes all input to that of a string ^.~
$haystack = ' '.$haystack;
if (!strpos($haystack, $needle))
return false;
$offset=0;
for($i = 1; $i < $nth; $i++)
$offset = strpos($haystack, $needle, $offset) + 1;
return strpos($haystack, $needle, $offset) - 1;
}
?>


webkami

You can easily skip these two meaningless comment lines in my last function.These comments are for old version of the function, I was not using length of needle in that version. Code is fine itself, I suppose ;)
<?
...
//start $pos from -1, cause we are adding 1 into it while searchig
//so the very first iteration will be 0
...
?>


admin

Yay! I came up with a very useful function. This finds a beginning marker and an ending marker (the first after the beginning marker), and returns the contents between them. You specify an initial position in order to tell it where to start looking. You can use a while() or for() loop to get all occurence of a certain string within a string (for example, taking all hyperlinks in a string of HTML code)...
function get_middle($source, $beginning, $ending, $init_pos) {
$beginning_pos = strpos($source, $beginning, $init_pos);
$middle_pos = $beginning_pos + strlen($beginning);
$ending_pos = strpos($source, $ending, $beginning_pos + 1);
$middle = substr($source, $middle_pos, $ending_pos - $middle_pos);
return $middle;
}
For example, to find the URL of the very first hyperlink in an HTML string $data, use:
$first_url = get_middle($data, '<a href="', '"', 0);
It's done wonders for scraping HTML pages with certain tools on my website.


horst

Why in such a way?
<?
$txt = preg_replace("/<script(.*)<\/script>/sim", "", $txt);
?>


php .at. wwwcrm .dot. com

Watch out for type!
The following code will return "not matched", which is a little counter-intuitive.
<?php
$val1=123;
$val2="123,456,789";
if (strpos($val2, $val1)!==false) echo "matched";
else echo "not matched";
?>
When $val1 is cast to string, it behaves as you might expect:
<?php
$val1=(string)123;
$val2="123,456,789";
if (strpos($val2, $val1)!==false) echo "matched";
else echo "not matched";
?>
Hope this saves someone the couple of hours that it took me to spot it :-)
Regards,
Alex Poole


jamie

Try this function to find the first position of needle before a given offset.
For example:
<?php
$s = "This is a test a is This";
$offset = strpos($s, "test");
strnpos($s, "is", $offset);  // returns 17
strnpos($s, "is", -$offset); // returns 5
// Works just like strpos if $offset is positive.
// If $offset is negative, return the first position of needle
// before before $offset.
function strnpos($haystack, $needle, $offset=0)
{
if ($offset>=0)
$result=strpos($haystack, $needle, $offset);
else
{
$offset=strlen($haystack)+$offset;
$haystack=strrev($haystack);
$needle=strrev($needle);
$result=strpos($haystack, $needle, $offset);
if ($result!==false)
{
$result+=strlen($needle);
$result=strlen($haystack)-$result;
}
}
return $result;
}
?>


banana dot meal

To thepsion5 at hotmail dot com:
Please mind the warning part of the documentation!
Your function won't work on $Haystack s starting with $needle.
Here's a solution for that:
<?
function findAllOccurences($Haystack, $needle, $limit=0)
{
 $Positions = array();
 $currentOffset = 0;
 $count=0;
 while(($pos = strpos($Haystack, $needle, $offset))!==false && ($count < $limit || $limit == 0))
 {
  $Positions[] = $pos;
  $offset = $pos + strlen($needle);
  $count++;
 }
 return $Positions;
}
?>


18-feb-2006 11:48

this works fine:
function getCurrentBrowser() {
$browser = $_SERVER['HTTP_USER_AGENT'];

if (strpos(strtoupper($browser), 'MSIE') !== false) {
return "Internet Explorer";
} else if (strpos(strtoupper($browser), 'FIREFOX') !== false) {
return "Firefox";
} else if (strpos(strtoupper($browser), 'KONQUEROR') !== false) {
return "Konqueror";
} else if (strpos(strtoupper($browser), "LYNX") !== false) {
return "Lynx";
} else {
return $browser;
}
}
if $browser ist
"Lynx/2.8.5rel.2 libwww-FM/2.14 SSL-MM/1.4.1 OpenSSL/0.9.7g"
its work fine, too.


14-jan-2007 12:52

this is nice you are so excited but parsing href=" will never really work.
remember whitespaces


user

This is a bit more useful when scanning a large string for all occurances between 'tags'.
<?php
function getStrsBetween($s,$s1,$s2=false,$offset=0) {
/*====================================================================
Function to scan a string for items encapsulated within a pair of tags
getStrsBetween(string, tag1, <tag2>, <offset>
If no second tag is specified, then match between identical tags
Returns an array indexed with the encapsulated text, which is in turn
a sub-array, containing the position of each item.
Notes:
strpos($needle,$haystack,$offset)
substr($string,$start,$length)
====================================================================*/
if( $s2 === false ) { $s2 = $s1; }
$result = array();
$L1 = strlen($s1);
$L2 = strlen($s2);
if( $L1==0 || $L2==0 ) {
return false;
}
do {
$pos1 = strpos($s,$s1,$offset);
if( $pos1 !== false ) {
$pos1 += $L1;
$pos2 = strpos($s,$s2,$pos1);
if( $pos2 !== false ) {
$key_len = $pos2 - $pos1;
$this_key = substr($s,$pos1,$key_len);
if( !array_key_exists($this_key,$result) ) {
$result[$this_key] = array();
}
$result[$this_key][] = $pos1;
$offset = $pos2 + $L2;
} else {
$pos1 = false;
}
}
} while($pos1 !== false );
return $result;
}
?>


vig0

This function takes in a string and a delimiter. It then builds an array of the data in between the delimiter then returns the array. I needed this function for something that I'm doing and thought it was somewhat useful.
function multiStrposArray($haystack, $needle){
 $array = array();
 $row   = 0;
 $var   = "";
 for ($i=0; $i<strlen($haystack); $i++) {
   if (substr($haystack, $i, 1)==$needle) {
     $array[$row] = $var;
     $row++;
     $var = "";
   } else {
     $var .= substr($haystack, $i, 1);
   }
 }
 if ($var) $array[$row] = $var;
 return $array;
}


info

this function takes a space-deliminted string as a list of potential needles and runs it against another string as a haystack.
the number of positive matches of needles within the haystack is returned as a rounded percentile.
function keyMatch($needles,$haystack) {
$nArray=split(" ",$needles);
$found=0;
$best=count($nArray);
  for($i=0;$i<count($nArray);$i++) {
$pzn=strpos(strtoupper($haystack),strtoupper($nArray[$i]));
if ($pzn>-1) { $found++; }
  }
$pct=($found*100)/$best;
return round($pct);
}
$test = keyMatch("the quick wolf","the quick brown fox jumps over the lazy dog");
echo($test);
RESULT:
67
(because "the" and "quick" were found but "wolf" was not)


gal_chen123

this function returns the text between 2 strings:
function get_between ($text, $s1, $s2) {
$mid_url = "";
$pos_s = strpos($text,$s1);
$pos_e = strpos($text,$s2);
for ( $i=$pos_s+strlen($s1) ; ( ( $i < ($pos_e)) && $i < strlen($text) ) ; $i++ ) {
$mid_url .= $text[$i];
}
return $mid_url;
}
if $s1 or $s2 are not found, $mid_url will be empty
to add an offset, simply compare $pos_s to the offset, and only let it continue if the offset is smaller then $pos_s.


rycardo74

this function return all src properties from a html text
in array you can filter the specifics html tags with strip_tags
$HTML=strip_tags ( $HTML, '<img>' );
$tag = trip_tag_prop("src=\"" , "\"" , $HTML);
function trip_tag_prop($ini,$end,$HTML ){
$ini_len= strlen($ini);
$end_len= strlen($end);
$inizio_pos=0;
while($inizio_pos  = strpos ( $HTML, $ini, $inizio_pos)){
$fine_pos     = strpos ( $HTML, $end,($inizio_pos + $ini_len));
$tag[]        = substr ( $HTML, $inizio_pos + $ini_len ,($fine_pos - $inizio_pos - $ini_len) );
$inizio_pos=$fine_pos;
}
return $tag;
}
by :Auayama , cabrera  rycardo74 (a) gmail (dot) com


samuraj

there was a code (from wodzuY2k at interia dot pl) removing all between <script> tags..
but it didn't work if the tag begins like <SCRIPT language=javascript type=text/javascript>
here is function removing all between "<script"  and  "/script>"
<?php
function remove_js($contents)
{
 while(true)
 {
  $begPos = strpos($contents,"<script");
  if ($begPos===false) break; //all tags were found & replaced.
  $endPos = strpos($contents,"/script>",$begPos+strlen("<script"));
  $tmp = substr($contents,0,$begPos);
  $tmp .= substr($contents,$endPos+strlen("script>"));
  $contents = $tmp;
  if ($loopcontrol++>100) break; //loop infinity control
  continue;  //search again
 }
 return $contents;
}
?>


johnjc-phpdocs

The === and !== are not fully documented in either the Comparison Operator, Booleans type sections. They are talked about a bit more in the sections on strpos() and array_search() but they refer you to the section on Booleans for further information.
I am putting my contribution on === and !== in the Booleans section with pointers to it from the comment areas of other sections.
http://uk.php.net/manual/en/language.types.boolean.php


14-apr-2007 03:18

Thanks to spinicrus (see above) I have sorted out a problem that was bugging me for ages. I have a routine in Etomite Content Management System that will display a set number of characters of a news item and invite visitors to "Read more".
Unfortunately the 400 character summary sometimes displayed a partial word at the end.
Using the following code based on spinicrus's exampleI have now overcome this.
#################################
#only full word at the end
   $string=$rest;
   $charToFind=" ";
   $searchPos = $lentoshow;
   $searchChar = '';
   //
   while ($searchChar != $charToFind) {
       $newPos = $searchPos-1;
       $searchChar = substr($string,$newPos,strlen($charToFind));
       $searchPos = $newPos;
   }
$rest=substr($string,0,$searchPos)." ";
################################


benjie

str_replace evaluates its arguments exactly once.
for example:
<?php
$page = str_replace("##randompicture##", getrandompicture(), $page);
?>
will call getrandompicture() once, ie it will insert the same random picture for each occurrence of ##randompicture## :(
Here is my quick and dirty workaround:
<?php
function add_random_pictures($text) {
 while (($i = strpos($text, "##randompicture##")) !== false) {
   $text = substr_replace($text, getrandompicture(), $i, strlen("##randompicture##"));
 }
 return $text;
}
$page = add_random_pictures($page);
?>


webkami

Str Pos Nth (Position of nth occurance of a string)
A handy function to get the position of nth occurance of a substring in a string, with an optional param to make it case insenstive. I am calling it strposnth, suggestions welcome.
Third optional parameter gets the value of n, e.g puting in 2 will return position of second occurance of needle in haystack: Valid inputs (1 = default) 2,3,4.....
Fourth optional parameter can be used to specify the function as case insenstive: Valid inputs (0 = case senstive = default) 1 = case insenstive.
Code:
<?
function strposnth($haystack, $needle, $nth=1, $insenstive=0)
{
//if its case insenstive, convert strings into lower case
if ($insenstive) {
$haystack=strtolower($haystack);
$needle=strtolower($needle);
}
//count number of occurances
$count=substr_count($haystack,$needle);

//first check if the needle exists in the haystack, return false if it does not
//also check if asked nth is within the count, return false if it doesnt
if ($count<1 || $nth > $count) return false;

//run a loop to nth number of accurance
//start $pos from -1, cause we are adding 1 into it while searchig
//so the very first iteration will be 0
for($i=0,$pos=0,$len=0;$i<$nth;$i++)
{
//get the position of needle in haystack
//provide starting point 0 for first time ($pos=0, $len=0)
//provide starting point as position + length of needle for next time
$pos=strpos($haystack,$needle,$pos+$len);
//check the length of needle to specify in strpos
//do this only first time
if ($i==0) $len=strlen($needle);
}

//return the number
return $pos;
}
?>
I just construct this function after trying to search a similar one to use in a shopping cart. I am using this to display a limited number of lines or text for featured products. My aim is to limit the product description to 100 characters or 3 lines / 3 list items whichever is less.
Example code goes like this
<?
//get the product description from recordset
$text=$row['product_desc'];
//strip off text if its longer than 100 characters
if (strlen($text)>100) $text=substr($text,0,100)." ...";
//get ending of the third line
$pos=strposnth($text,"\n",3,1);
//if found, strip off text after that
if($pos) $text=substr($text,0,$pos);
//nl2li (new line 2 list) this function converts the \n seprated lines of text into sorted or unsorted lists
//I have posted this function in nl2br
//http://uk2.php.net/manual/en/function.nl2br.php
$text=nl2li($text);
echo $text;
?>
Examples:
strposnth("I am trying to go now.","o"); // returns 13 (strpos behavior)
strposnth("I am trying to go now.","O"); // returns false (strpos behavior)
strposnth("I am trying to go now.","o",2); // returns 16 (second occurance)
strposnth("I am trying to go now.","o",7); // returns false (occurance count is less than 7)
strposnth("I am trying to go now.","O",1,1); // returns 13 (stripos behavior)
strposnth("I am trying to go now.","O",3,1); // returns 19 (stripos behavior + nth occurance)
Suggestions and corrections are welcome.
Regards,
webKami [at] akdomains.com


charles

Small improvement on the efforts of others:
<?php
function strpos_all($hs_haystack, $hs_needle, $hn_offset = 0, $hn_limit = 0) {
 $ha_positions = array();
 $hn_count = 0;
 while (false !== ($pos = strpos($hs_haystack, $hs_needle, $hn_offset)) && ($hn_limit == 0 || $hn_count < $hn_limit)) {
   $ha_positions[] = $pos;
   $hn_offset = $pos + strlen($hs_needle);
   ++$hn_count;
   }
 return $ha_positions;
 }
function preg_pos($hs_pattern, $hs_subject, &$hs_foundstring, $hn_offset = 0) {
 $hs_foundstring = NULL;
   
 if (preg_match($hs_pattern, $hs_subject, $ha_matches, PREG_OFFSET_CAPTURE, $hn_offset)) {
   $hs_foundstring = $ha_matches[0][0];
   return $ha_matches[0][1];
   }
 else {
   return FALSE;
   }
 }
function preg_pos_all($hs_pattern, $hs_subject, &$ha_foundstring, $hn_offset = 0, $hn_limit = 0) {
 $ha_positions = array();
 $ha_foundstring = array();
 $hn_count = 0;
 while (false !== ($pos = preg_pos($hs_pattern, $hs_subject, $hs_foundstring, $hn_offset)) && ($hn_limit == 0 || $hn_count < $hn_limit)) {
   $ha_positions[] = $pos;
   $ha_foundstring[] = $hs_foundstring;
   $hn_offset = $pos + 1;                     // alternatively: '$pos + strlen($hs_foundstring)'
   ++$hn_count;
   }
 return $ha_positions;
 }
print_r(preg_pos_all('/s...s/', "she sells sea shells on the sea floor", $ha_matches));
print_r($ha_matches);
?>


spam

Simple function to determine if a needle occurs in a haystack
function is_substr($needle, $haystack){
       $pos = strpos($haystack, $needle);

       if ($pos === false) {
               return false;
       } else {
               return true;
       }
}


yess

Regarding James Perlman's findStr(), it's very nice although doesnt always return true when it should.  I found fnmatch() to be a more accurate function for 'wildcard matching'. I'm building an IRCd and require wildmatching for many different commands. Hope this helps somone.
<?
findStr("12.12.12.*","12.12.12.12"); //returns null, should return true
fnmatch("12.12.12.*","12.12.12.12"); //returns 1.
?>


philip

Many people look for in_string which does not exist in PHP, so, here's the most efficient form of in_string() (that works in both PHP 4/5) that I can think of:
<?php
function in_string($needle, $haystack, $insensitive = 0) {
if ($insensitive) {
return (false !== stristr($haystack, $needle)) ? true : false;
} else {
return (false !== strpos($haystack, $needle))  ? true : false;
}
}
?>


hs_at_duijst_dot_com

Just to be clear: unlike stripos(), strpos() is case-sensitive.

wormss

Im sure there are more efficient methods of this, but i use this alot when dealing with rss and was proud of it.
<?
function data_from_element($needle,$haystack,$tags=FALSE) { // Expects two Strings, returns Array
$needle_start = "<".$needle.">"; $needle_end = "</".$needle.">";
$array = array(); $pos_start = 0;
while(($pos_start = strpos($haystack,$needle_start,$pos_start)) !== false) {
$pos_end = strpos($haystack,$needle_end,$pos_start);
if($tags) $array[] = substr($haystack,$pos_start,$pos_end-$pos_start+strlen($needle_end));
else $array[] = substr($haystack,$pos_start + strlen($needle_start),$pos_end - $pos_start - strlen($needle_start));
$pos_start++;
}
return $array;
}
d
//example
$rss = '<?xml version="1.0"?> <rss version="2.0"> <channel> <title>Example RSS</title> <description>Example RSS Description</description> <link>http://example.com/rss/</link> <item> <title>Example RSS 1</title> <link>http://example.com/rss/1.html</link> <description>Example 1</description> </item> <item> <title>Example RSS 2</title> <link>http://example.com/rss/2.html</link> <description>Example 2</description> </item> </channel> </rss>';
$items = data_from_elements(link,$rss); // $rss[0] => "http://example.com/rss/"
$items = data_from_elements(link,$rss,true); // $rss[0] => "<link> http://example.com/rss/ </link>"
?>


steve

If you're wanting a simple "strpos" using a pattern and don't need the complexity of multi_strpos below, this function (for PHP 4.3.0+) returns the regex position.
 function preg_pos($sPattern, $sSubject, &$FoundString, $iOffset = 0) {
  $FoundString = NULL;
 
  if (preg_match($sPattern, $sSubject, $aMatches, PREG_OFFSET_CAPTURE, $iOffset) > 0) {
   $FoundString = $aMatches[0][0];
   return $aMatches[0][1];
  }
  else {
   return FALSE;
  }
 }
It also returns the actual string found using the pattern, via $FoundString.


spinicrus

if you want to get the position of a substring relative to a substring of your string, BUT in REVERSE way:
<?php
function strpos_reverse_way($string,$charToFind,$relativeChar) {
//
$relativePos = strpos($string,$relativeChar);
$searchPos = $relativePos;
$searchChar = '';
//
while ($searchChar != $charToFind) {
$newPos = $searchPos-1;
$searchChar = substr($string,$newPos,strlen($charToFind));
$searchPos = $newPos;
}
//
if (!empty($searchChar)) {
//
return $searchPos;
return TRUE;
}
else {
return FALSE;
}
//
}
?>


arias

If you want to get all positions in an array, you can use this function. If the optional parameter count is especified, the function will put there the number of matches.
function strallpos($pajar, $aguja, $offset=0, &$count=null) {
 if ($offset > strlen($pajar)) trigger_error("strallpos(): Offset not contained in string.", E_USER_WARNING);
 $match = array();
 for ($count=0; (($pos = strpos($pajar, $aguja, $offset)) !== false); $count++) {
   $match[] = $pos;
   $offset = $pos + strlen($aguja);
 }
 return $match;
}


christian dot nei_til_spam

If you want to find the position of the first character succeeding $needle, this function does it for you. $offset is optional.
<?php
function strpos_succ($haystack, $needle) {
$offset = (func_num_args() > 2) ? func_get_arg(2) : 0;
$res = strpos($haystack, $needle, $offset);
return (($res === false) ? false : $res + strlen($needle));
}
?>
Example:
<?php
$str = 'Mighty <a href="nowhere.htm">useful</a> function, this strpos_succ!';
//Where does the first <a> tag in $str end?
$aPos = strpos_succ($str, ">");
// $aPos=29
?>


virtual hunter root

If you want to find positions of all needle's in haystack,
you can use this one:
while (($pos=strpos($haystack,$needle,$pos+1))!==false) $pos_array[$i++]=$pos;
But mind, that it will find from second char. You must use $pos=-1; before you want search from first char.
{
$haystack="one two three one two three one two three one two three one";
$needle="one";
$pos=-1;
while (($pos=strpos($haystack,$needle,$pos+1))!==false) $pos_array[$i++]=$pos;
}
RESULT:
$pos_array[0] = 0
$pos_array[1] = 14
$pos_array[2] = 28
$pos_array[3] = 42
$pos_array[4] = 56


sinai

If you want to check for either IE6 or 7 individually.
<?php
function browserIE($version)
{
 if($version == 6 || $version == 7)
 {
   $browser = strpos($_SERVER['HTTP_USER_AGENT'], "MSIE ".$version.".0;");
         
   if($browser == true)
   {
     return true;
   }
   else
   {
     return false;
   }
 else
 {
   return false;
}
?>


dale

if you want need a fast function to find the first occurrence of any ch element of an needle array this function might be of use:
<?php
$eurl = strpos_needle_array($text, array('"'=>0,'\''=>0,'>'=>0, ' '=>0, "\n"=>0), $surl);
function strpos_needle_array(& $text, $needle_ary, $offset=0){
for($ch_pos=$offset;$ch_pos<strlen($text);$ch_pos++){
if(isset($needle_ary[$text[$ch_pos]])){
return $ch_pos;
}
}
return false;
}
?>


wagner christian

If you plan to use an integer as needle you need first to convert your integer into a String else it's not going to work.
For exemple :
<?php
$id  = 1;
$my_text = "hel124lo";
$first_position =strpos($my_text ,substr($id,0));
?>
There are for sure some another solutions to convert an integer into a string in php.


leibwaechter

If you only want to look, if a string appears in another string - even at position 0 - , you can also use substr_count():
<?php
 $mystring = "Hello Chris";
 if (substr_count($mystring, "Hello") == 0)
   echo "no";
 // same as:
 if (strpos($mystring, "Hello") === false)
   echo "no";
?>


koteskie

I've been looking at previous posts and came up with this function to find the start and end off an certain occurance or all occurances of needle within haystack.
I've made some minor tweaks to the code itself, like  counting the length of needle only once and counting the result set array instead of using a count variable.
I also added a length parameter to the result set to use in a following substr_replace call etc...
<?php
function strpos_index($haystack = '',$needle = '',$offset = 0,$limit = 99,$return = null)
{
$length = strlen($needle);
$occurances = array();
while((($count = count($occurances)) < $limit) && (false !== ($offset = strpos($haystack,$needle,$offset))))
{
$occurances[$count]['length'] = $length;
$occurances[$count]['start'] = $offset;
$occurances[$count]['end'] = $offset = $offset + $length;
}
return $return === null ? $occurances : $occurances[$return];
}

?>


mvp

I understand the excitement of "admin at xylotspace dot com."  I wrote three functions that I use in EVERY website I develop.  What they do is get the text between strings.  I made them case-insensitive (for php < 5) using "strtolower." This would not be necessary if you used "stripos."  Now the first function is close to what "admin at xylotspace dot com" wrote, but does not have the position element.  It also will return an empty string if no substring was found.  If you want to get the title of an HTML document use:
TextBetween('<title>','</title>',$content);
The second function was revolutionary for me, because it gets an array of items between pairs of strings.  So, with that I can grab most XML lists, or get all the links or images in a document.  All the links in a document could be found using:
TextBetweenArray('href="','"',$content);
The third is less used, but is useful to process an array and get substrings within each record.
//-----GET TEXT BETWEEN STRINGS------
function TextBetween($s1,$s2,$s){
 $s1 = strtolower($s1);
 $s2 = strtolower($s2);
 $L1 = strlen($s1);
 $scheck = strtolower($s);
 if($L1>0){$pos1 = strpos($scheck,$s1);} else {$pos1=0;}
 if($pos1 !== false){
   if($s2 == '') return substr($s,$pos1+$L1);
   $pos2 = strpos(substr($scheck,$pos1+$L1),$s2);
   if($pos2!==false) return substr($s,$pos1+$L1,$pos2);
 }
 return '';
}
//-----GET ARRAY TEXT BETWEEN STRINGS------
function TextBetweenArray($s1,$s2,$s){
 $myarray=array();
 $s1=strtolower($s1);
 $s2=strtolower($s2);
 $L1=strlen($s1);
 $L2=strlen($s2);
 $scheck=strtolower($s);
 do{
 $pos1 = strpos($scheck,$s1);
 if($pos1!==false){
   $pos2 = strpos(substr($scheck,$pos1+$L1),$s2);
   if($pos2!==false){
     $myarray[]=substr($s,$pos1+$L1,$pos2);
     $s=substr($s,$pos1+$L1+$pos2+$L2);
     $scheck=strtolower($s);
     }
}
 } while (($pos1!==false)and($pos2!==false));
 return $myarray;
}
//-----GET SUBTEXT IN ARRAY ITEMS------
function SubTextBetweenArray($s1,$s2,$myarray){
 for ($i=0; $i< count($myarray); $i++)
  {$myarray[$i]=TextBetween($s1,$s2,$myarray[$i]);}
 return $myarray;
}


wolfeym38

I finally figured out how to use this function correctly (and efficiently) if you want to test for a needle that may start at the beginning of haystack, simply use
if (strpos($haystack, $needle) === 0)) {
 do stuff here..
}
someone else mentioned that you needed to assign a variable first and test to make sure that it was === true first.. That is not needed


thepsion5

I created this little function based on the one posted by chasesan at gmail dot com; It find all occurences of a string within another string and returns their positions as an array:
<?PHP
function findAllOccurences($Haystack, $needle, $limit=0)
{
 $Positions = array();
 $currentOffset = 0;
 $count=0;
 while(($pos = strpos($Haystack, $needle, $offset)) && ($count < $limit || $limit == 0))
 {
   $Positions[] = $pos;
   $offset = $pos + strlen($needle);
   $count++;
 }
 return $Positions;
}
?>
I hope this helps someone :)


admin

I created a useful function that returns an array with the positions within a string. For more info, read the comments:
<?php
// Returns an array in this fashion:
// array(count => position)
function strpos_array($haystack, $needle){
  $kill = 0; // Kills while loop when changed
  $offset = 0; // Offset for strpos()
  $i = 0; // Counter, not iterator
  while ($kill === 0) {
  $i++;
  $result = strpos($haystack, $needle, $offset);
  if ($result === FALSE) { // If result is false (no more instances found), kill the while loop
  $kill = 1;
  } else {
  $array[$i] = $result; // Set array
  $offset = $result + 1; // Offset is set 1 character after previous occurence
  }
  }
  return $array;
}
?>


ygrange

Hmm. I think the maker of multi_strpos forgot to add that it allowed a negative position (if $n=0, $position[($n-1)] = $position[-1]. I just changed a slightly bit of the code to make it really correct.
I just replace
$position[$n] = (strlen($fragment[0]) + $position[($n-1)]);
by
$position[$n] = (strlen($fragment[0]) + $positione[$n]);
$positione[$n+1]=$position[$n];


arduenn

Hi all,
This function returns an array of positions (as integers) of a regular expression pattern that could occur several times within a string (or FALSE if the pattern does not occur). Note that the function is able to determine the positions of overlapping patterns. There may be shorter ways of determining multiple pattern positions (such as by using 'explode') but likely these won't find the overlapping patterns.
The multi_strpos function has one restriction (for the sake of snippet economy): if you use a pattern starting with '\*' (backslash asterisk) a hit within the string will invoke an infinite loop.
This function was initially written to create restriction maps of DNA sequences but you may find other uses.
<?
function multi_strpos($pattern, $sequence) {
 $n = -1;
 while (ereg($pattern, $sequence)) {
   $n++;
   $fragment = split($pattern, $sequence);
   $trimsize = (strlen($fragment[0]))+1;
   $sequence = "*".substr($sequence, $trimsize);
   $position[$n] = (strlen($fragment[0]) + $position[($n-1)]);}
 return $position;}
// Below some code to demonstrate the function.
$testsequence = "She sells sea shells at the see shore.";
echo "Test sequence = '$testsequence'\n\n";
$testpattern = "s...s";
echo "Regular expression pattern = '$testpattern'\n\n";
$position = multi_strpos($testpattern, $testsequence);
if ($position) {
 echo "Pattern match found at:\n";
 while (list($index, $pos) = each($position)) {
   echo "$pos\n";}}
?>


rich deeson

Here's a somewhat more efficient way to truncate a string at the end of a word. This will end the string on the last dot or last space, whichever is closer to the cut off point. In some cases, a full stop may not be followed by a space eg when followed by a HTML tag.
<?php
$shortstring = substr($originalstring, 0, 400);
$lastdot = strrpos($shortstring, ".");
$lastspace = strrpos($shortstring, " ");
$shortstring = substr($shortstring, 0, ($lastdot > $lastspace? $lastdot : $lastspace));
?>
Obviously, if you only want to split on a space, you can simplify this:
<?php
$shortstring = substr($originalstring, 0, 400);
$shortstring = substr($shortstring, 0, strrpos($shortstring, " "));
?>


ed lecky-thompson

Here's a quick function which can replace strtotime, and will work fine on dates pre-1970 (i.e. it will return a negative number as expected).
This negative time stamp seems to be supported as an input parameter by methods like date() up to a point, but if you get crazy and start talking about dates in the 1700s (everybody was using PHP3 back then, of course) it gets upset.
For those of you doing staff databases and so forth, of course, this is probably fine - it's definitely OK for any dates post 1900, and this value has been hard coded into the function below.
function safestrtotime($strInput) {
$iVal = -1;
for ($i=1900; $i<=1969; $i++) {
# Check for this year string in date
$strYear = (string)$i;
if (!(strpos($strInput, $strYear)===false)) {
$replYear = $strYear;
$yearSkew = 1970 - $i;
$strInput = str_replace($strYear, "1970", $strInput);
};
};
$iVal = strtotime($strInput);
if ($yearSkew > 0) {
$numSecs = (60 * 60 * 24 * 365 * $yearSkew);
$iVal = $iVal - $numSecs;
$numLeapYears = 0; # Work out number of leap years in period
for ($j=$replYear; $j<=1969; $j++) {
$thisYear = $j;
$isLeapYear = false;
# Is div by 4?
if (($thisYear % 4) == 0) {
$isLeapYear = true;
};
# Is div by 100?
if (($thisYear % 100) == 0) {
$isLeapYear = false;
};
# Is div by 1000?
if (($thisYear % 1000) == 0) {
$isLeapYear = true;
};
if ($isLeapYear == true) {
$numLeapYears++;
};
};
$iVal = $iVal - (60 * 60 * 24 * $numLeapYears);
};
return($iVal);
};


takapz

here a little function for tag parsing
function parsing($tag,$string) {
$start=strpos($string,"<" . $tag . ">" );
$start=$start + strlen("<" . $tag . ">");
$end=(strpos($string, "</" . $tag . ">"));
$num=  ($end - $start);
$valore=substr($string,$start,$num);
 return $valore;
}


lindbeer

He he. I've got an updated version of the multiStrposArray function:
<?php
function multiStrposArray($haystack, $needle) {
   return explode($needle, $haystack);
}
?>


iceye

Get text between $s1 and $s2, return an array contains every occurrence (based on code of old comment/s but with offset and without strtolower)
Sample:
$myDivsContent = getStrsBetween("<div","</div>",$myHtmlSrc);
Sample:
or...get rows for html table
...
...
//using TextBetween from old comment...
$aTable = TextBetween("<table","</table>",$myHtmlSrc);
$rows = getStrsBetween("<tr","</tr>",$aTable);
...
...
function getStrsBetween($s1,$s2,$s,$offset=0){
 $result = array();
 $index= 0;
 $L1 = strlen($s1);
 $found = false;
 do{
if($L1>0){
$pos1 = strpos($s,$s1,$offset);
}
else {
$pos1=$offset;
}
if($pos1 !== false){
if($s2 == '')
$result[$index++]= substr($s,$pos1+$L1);
$pos2 = strpos(substr($s,$pos1+$L1),$s2,$L1);
if($pos2!==false){
$result[$index++]= substr($s,$pos1+$L1,$pos2);
$offset += $pos2 + strlen($s2);
}
else{
$pos1 = false;
}
}
 }while($pos1 !== false);
 return $result;
}

BUGs/Problems:
Function do not stop while $s1 is found in $s.


justin

Function:
stripos_words($haystack,'words in string')
This function finds and reports positions of all words in supplied haystack. It returns the results as an array. The array has the following structure.
Array
(
   [69] => Array
       (
           [start] => 69
           [end] => 74
           [word] => honey
       )
   [226] => Array
       (
           [start] => 226
           [end] => 232
           [word] => cobweb
       )
}
Where, for convenience, the main key also contains the positions of each found word occurrence.
If you want the main key to be 0,1,2,3,etc then set the third parameter ($pos_as_key) to false;
Hope this is of help to someone.
Cheers,
Justin :)
<?php
function stripos_words($haystack,$needles='',$pos_as_key=true)
{
$idx=0; // Used if pos_as_key is false

// Convert full text to lower case to make this case insensitive
$haystack = strtolower($haystack);

// Split keywords and lowercase them
foreach ( preg_split('/[^\w]/',strtolower($needles)) as $needle )
{
// Get all occurences of this keyword
$i=0; $pos_cur=0; $pos_found=0;
while (  $pos_found !== false && $needles !== '')
{
// Get the strpos of this keyword (if thereis one)
$pos_found = strpos(substr($haystack,$pos_cur),$needle);
if ( $pos_found !== false )
{
// Set up key for main array
$index = $pos_as_key ? $pos_found+$pos_cur : $idx++;

// Populate main array with this keywords positional data
$positions[$index]['start'] = $pos_found+$pos_cur;
$pos_cur += ($pos_found+strlen($needle));
$positions[$index]['end']   = $pos_cur;
$positions[$index]['word'] = $needle;
$i++;
}
}
}
// If we found anything then sort the array and return it
if ( isset($positions) )
{
ksort($positions);
return $positions;
}
// If nothign was found then return false
return false;
}
?>


csaba

function nthPos ($str, $needles, $n=1) {
   // finds the nth occurrence of any of $needles' characters in $str
   //  returns -1 if not found; $n<0 => count backwards from end
   //  e.g. $str = "c:\\winapps\\morph\\photos\\Party\\Phoebe.jpg";
   //       substr($str, nthPos($str, "/\\:", -2)) => \Party\Phoebe.jpg
   //       substr($str, nthPos($str, "/\\:", 4)) => \photos\Party\Phoebe.jpg
   $pos = -1;
   $size = strlen($str);
   if ($reverse=($n<0)) { $n=-$n; $str = strrev($str); }
   while ($n--) {
$bestNewPos = $size;
for ($i=strlen($needles)-1;$i>=0;$i--) {
   $newPos = strpos($str, $needles[$i], $pos+1);
   if ($newPos===false) $needles = substr($needles,0,$i) . substr($needles,$i+1);
   else $bestNewPos = min($bestNewPos,$newPos); }
if (($pos=$bestNewPos)==$size) return -1; }
   return $reverse ? $size-1-$pos : $pos;
}
Csaba Gabor from Vienna


ebypdx

counting the occurrences of a substring, recursive-style instead of looping.
<?
function countSubstrs($haystack, $needle)
{
return (($p = strpos($haystack, $needle)) === false) ? 0 : (1 + countSubstrs(substr($haystack, $p+1), $needle));
}
?>


bishop

Code like this:
<?php
if (strpos('this is a test', 'is') !== false) {
   echo "found it";
}
?>
gets repetitive, is not very self-explanatory, and most people handle it incorrectly anyway. Make your life easier:
<?php
function str_contains($haystack, $needle, $ignoreCase = false) {
   if ($ignoreCase) {
       $haystack = strtolower($haystack);
       $needle   = strtolower($needle);
   }
   $needlePos = strpos($haystack, $needle);
   return ($needlePos === false ? false : ($needlePos+1));
}
?>
Then, you may do:
<?php
// simplest use
if (str_contains('this is a test', 'is')) {
   echo "Found it";
}
// when you need the position, as well whether it's present
$needlePos = str_contains('this is a test', 'is');
if ($needlePos) {
   echo 'Found it at position ' . ($needlePos-1);
}
// you may also ignore case
$needlePos = str_contains('this is a test', 'IS', true);
if ($needlePos) {
   echo 'Found it at position ' . ($needlePos-1);
}
?>


anonymous

Beware of following the examples too closely...
$str = "abcdef";
echo strpos($str, 'b'); // outputs 1
however
$str = "abc\nef";
echo strpos($str, '\n'); // output nothing (FALSE)
Not a great way to try to stop PHP email injection attacks (as I found out).
Instead use
strpos($str, "\n")
because the escapes are processed only when using double quotes.


talthen

Because I like the way pascal functions work, so I created this little function:
function paspos($base,$findme)
{
$result=strpos($base,$findme);
if ($result===false) $result=-1;
return $result;
}
It returns -1 if the $findme may not be found in $base, or position where $findme was found in $base.
Now when you want to check if $base starts from $findme you can just type:
if (paspos($string,$searchfor)==0)) ....
Hope you'll like it :)


fox_galih

Be carefull when your searching position is in first index. For example:
$newsbody = "

This is paragraph";
if( strpos( $newsbody, "

" ) == 0 ) {
  // do something
}
when strpos returns FALSE, this "if block" will be executed by PHP, because FALSE also has 0 value. So, you have to make it a little bit longer of code:
$pos = strpos( $newsbody, "

" );
if ( $pos != FALSE ) {
$pos = strpos( $newsbody, "

" );
if( $pos == 0 )
$newsbody = substr( $newsbody, 3 );
}


damy_belthazor86

Aw.. I forgot to post the usage of my function :)
Here's an example in which an array is filled of all the values of the attribute src of the tag img.
<?php
$imgSrcValues = getTagAttributeValues($rteHtml,"<IMG","src=");
?>
bye!


ludvig dot ericson

As a simplified way of doing what the poster below did:
<?php
$firstName .= '\'';
if (!preg_match('/[sx]$/', $firstName)) {
   $firstName .= 's';
}
?>
If you feel using a regular expression is too much, try it - I've not tested yet, but I'd say preg_match() is faster then two strpos() calls.


kingbusiness

A simple function to find the number of occurances in a string within a string
<?php
function StringCount($searchstring, $findstring)
{
return (strpos($searchstring, $findstring) === false ? 0 :  count(split($findstring, $searchstring)) - 1);
}
?>


edykory

a shorter version of "admin at bwongar dot com" searching function:
function strpos_array($haystack, $needle){
 while (($pos = strpos($haystack, $needle, $pos)) !== FALSE)
                $array[] = $pos++;
 return $array;
}
I like "arias" version, but I guess strlen(aguja) can be cached in a local variable.


giulionospam

A further implementation of the great rstrpos function posted in this page. Missing some parameters controls, but the core seems correct.
<?php
// Parameters:
//
// haystack : target string
// needle   : string to search
// offset   : which character in haystack to start searching, FROM THE END OF haystack
// iNumOccurrence : how many needle to search into haystack beginning from offset ( i.e. the 4th occurrence of xxx into yyy )
function rstrpos ($haystack, $needle, $offset=0, $iNumOccurrence=1)
 {
 //
 $size = strlen ($haystack);
 $iFrom = $offset;
 $iLoop = 0;
 //
 do
   {
   $pos = strpos (strrev($haystack), strrev($needle), $iFrom);
   $iFrom = $pos + strlen($needle);
   }
 while ((++$iLoop)<$iNumOccurrence);
 //
 if($pos === false) return false;
 //
 return $size - $pos - strlen($needle);
 }
?>


cybertinus

@Wagner Christian:
Yes, there are better methods. The best is to just cast is. You cast like this:
<?php
$id = 1;
$string = (string) $id;
?>
If you var_dump() $string now you get the following output:
string(1) "1"
This is the recommended method. You're example should look like this then:
<?php
$id  = 1;
$my_text = "hel124lo";
$first_position =strpos($my_text , (string) $id);
?>


paul

@talthen at wp dot pl
Or you could just have typed:
if (strpos($string,$searchfor)===0)) ....
All you needed was en extra = instead of a whole function.


arachnion

<?php
//use a string as needle, even in PHP 4
//works the same like strrpos()
function stringrpos( $sHaystack, $sNeedle )
{
 $i = strlen( $sHaystack );
 while ( substr( $sHaystack, $i, strlen( $sNeedle ) ) != $sNeedle )
 {
   $i--;
   if ( $i < 0 )
   {
     return false;
   }
 }
 return $i;
}
?>


msandersen

<?PHP
$txt = preg_replace("|(<script.+</script>)|Usi", "", $txt);
?>
No point in keeping empty script tags. Ungreedy will prevent overzealous content removal where there are more than one set of script tags on the page. Multiline has no use here.
Otherwise, if you absolutely must have the empty tags:
<?PHP
$txt = preg_replace("|<script[^>]*>(.+)</script>|Usi", "", $txt);
?>
Martin


luko1

<?php  //prepocet velikosti souboru
function format($soubor) {
 $velikost = filesize($soubor);
 if($velikost < 1048576):
   return number_format(($velikost / 1024), 2, ',', ' ')." kB";
 else:
   return number_format(($velikost / 1024 / 1024), 2, ',', ' ')." MB";
 endif;
}
?>
<?php
function read_dir($dir)
{
 $path = opendir($dir);
 while (false !== ($file = readdir($path))):
   if ($file!="." && $file!=".."):
     if (is_file($dir."/".$file)):
       $items[] = $file;
     else:
       $items[] = $dir."/".$file;
     endif;
   endif;
 endwhile;
 closedir($path);
 if ($items):
   natcasesort($items);
   foreach ($items as $item){
     if (is_dir($item)):
       $dir1 = $item;
       $item1 = substr($item, strrpos($item, '/') + 1).'/';
       echo "<b>$item1</b><table border=1 style='margin-left: 60px; position: static; border: blue'><tr><td>";
       read_dir($dir1);
       echo "</table>";
     else:
       $cesta_file = "\"".$dir."/".$item."\"";
       $cesta_size = $dir."/".$item;
       echo "<span class=pop>&nbsp;<a href=$cesta_file title=$item>$item</a> <span> - ".format($cesta_size)."</span> </span>
";
     endif;
   } //endforeach
 endif;
}
?>
...
<table><tr><td>
<?php
$dir = ".";
read_dir($dir);
?>
</table>


08-mar-2005 12:32

<?
// You can use this to get the real path...
$include_path = getenv("PATH_TRANSLATED");
echo $include_path = substr($include_path, strpos($include_path, "/"), strrpos($include_path, "/"));
?>


adispam

$a="nice string 1234 with number";
$b=sprintf("nice string %d", 1234);
var_dump(strpos($a,$b));
returns FALSE.
substitute : $b=sprintf("nice string %d", 1234);
with: $b = "nice string "; $b.=(string)1234;
and you 'll get correct result (0)


php /at/ da.mcbf.net

"fox_galih at yahoo dot co dot uk" example below is absolutely not needed. As the example on this page shows, you can differentiate between position 0 and false by doing a strict comparison (===):
$newsbody = "

This is paragraph";
if( strpos( $newsbody, "

" ) === 0 ) {
  // will only get executed if string starts with


  // (false === 0) evaluates to false
}


Change Language


Follow Navioo On Twitter
addcslashes
addslashes
bin2hex
chop
chr
chunk_split
convert_cyr_string
convert_uudecode
convert_uuencode
count_chars
crc32
crypt
echo
explode
fprintf
get_html_translation_table
hebrev
hebrevc
html_entity_decode
htmlentities
htmlspecialchars_decode
htmlspecialchars
implode
join
levenshtein
localeconv
ltrim
md5_file
md5
metaphone
money_format
nl_langinfo
nl2br
number_format
ord
parse_str
print
printf
quoted_printable_decode
quotemeta
rtrim
setlocale
sha1_file
sha1
similar_text
soundex
sprintf
sscanf
str_getcsv
str_ireplace
str_pad
str_repeat
str_replace
str_rot13
str_shuffle
str_split
str_word_count
strcasecmp
strchr
strcmp
strcoll
strcspn
strip_tags
stripcslashes
stripos
stripslashes
stristr
strlen
strnatcasecmp
strnatcmp
strncasecmp
strncmp
strpbrk
strpos
strrchr
strrev
strripos
strrpos
strspn
strstr
strtok
strtolower
strtoupper
strtr
substr_compare
substr_count
substr_replace
substr
trim
ucfirst
ucwords
vfprintf
vprintf
vsprintf
wordwrap
eXTReMe Tracker