Possible Duplicate:
How to split a string in PHP at nth occurrence of needle?
Let's say I have a string variable:
$string = "1 2 3 1 2 3 1 2 3 1 2 3";
I want to cut off the end of this string starting at the fourth occurrence of the substring "2", so $string is now equal to this:
"1 2 3 1 2 3 1 2 3 1"
Effectively cutting of the fourth occurrence of "2" and everything after it. How would one go about doing this? I know how to count the number of occurrences with substr_count($string,"2");, but I haven't found anything else searching online.