PHP: How to find position of (user-defined) occurrence of a string using stripos -
there strpos() find first occurrence, , strrpos() find last occurrence.
this tutorial explained it's possible occurrence using loop, might not fast when haystack big. , code looking ugly now.
is there way find 2nd, 3rd, 4th, etc occurrence without looping on haystack? mean, finding required occurrence directly without looping? possible?
you can use regular expression, not faster looping strpos.
if (preg_match_all("/(match string)/g",$string,$matches)) { echo $matches[0] ; // whole string echo $matches[1] ; // first match echo $matches[2] ; // second match echo $matches[3] ; // , on }
if wan replace occurrences, use str_replace()
. way don't have worry offsets.
Comments
Post a Comment