Skip to content

Instantly share code, notes, and snippets.

@justinkelly
Created November 17, 2010 09:46
Show Gist options
  • Save justinkelly/703203 to your computer and use it in GitHub Desktop.
Save justinkelly/703203 to your computer and use it in GitHub Desktop.
kick ass strstr
<?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