Skip to content

Instantly share code, notes, and snippets.

@jesse1981
Created April 19, 2013 06:07
Show Gist options
  • Save jesse1981/5418437 to your computer and use it in GitHub Desktop.
Save jesse1981/5418437 to your computer and use it in GitHub Desktop.
Split a string at the offset of the nth needle.
function splitn($string, $needle, $offset) {
$newString = $string;
$totalPos = 0;
$length = strlen($needle);
for($i = 0; $i < $offset; $i++) {
$pos = strpos($newString, $needle);
// If you run out of string before you find all your needles
if($pos === false)
return false;
$newString = substr($newString, $pos+$length);
$totalPos += $pos+$length;
}
return array(substr($string, 0, $totalPos-$length),substr($string, $totalPos));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment