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.

Copyleft : LGPL
Idea by: webKami
Coded By: webKami

Version 1.0.0





Features:
1, Finds nth position of needle in haystack
2, Can perform an case senstive or insenstive operation

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.

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:

<?

//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)