Created
April 19, 2013 06:07
-
-
Save jesse1981/5418437 to your computer and use it in GitHub Desktop.
Split a string at the offset of the nth needle.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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