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:
<? |
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:
<? |
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)