Created
November 17, 2010 09:46
-
-
Save justinkelly/703203 to your computer and use it in GitHub Desktop.
kick ass strstr
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
<?php | |
function strstrbi($haystack, $needle, $before_needle=FALSE, $include_needle=TRUE, $case_sensitive=FALSE) { | |
//Find the position of $needle | |
if($case_sensitive) { | |
$pos=strpos($haystack,$needle); | |
} else { | |
$pos=strpos(strtolower($haystack),strtolower($needle)); | |
} | |
//If $needle not found, abort | |
if($pos===FALSE) return FALSE; | |
//Adjust $pos to include/exclude the needle | |
if($before_needle==$include_needle) $pos+=strlen($needle); | |
//get everything from 0 to $pos? | |
if($before_needle) return substr($haystack,0,$pos); | |
//otherwise, go from $pos to end | |
return substr($haystack,$pos); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment