note 79409 added to function.strstr

From: Date: Mon, 26 Nov 2007 00:42:05 +0000
Subject: note 79409 added to function.strstr
Groups: php.notes 
Request: Send a blank email to php-notes+get-132566@lists.php.net to get a copy of this message
I simplified prafe at prafesplace dot com's function: <?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); } ?> It's now 600 bytes, down from 2k. Also, here are replacements for strstr and stristr: <?php function strstr($haystack, $needle, $before_needle=FALSE) { //Find position of $needle or abort if(($pos=strpos($haystack,$needle))===FALSE) return FALSE; if($before_needle) return substr($haystack,0,$pos+strlen($needle)); else return substr($haystack,$pos); } function stristr($haystack, $needle, $before_needle=FALSE) { //Find position of $needle or abort if(($pos=strpos(strtolower($haystack),strtolower($needle)))===FALSE) return FALSE; if($before_needle) return substr($haystack,0,$pos+strlen($needle)); else return substr($haystack,$pos); } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 71.195.52.243 ---- Manual Page -- http://www.php.net/manual/en/function.strstr.php Edit -- https://master.php.net/note/edit/79409 Del: integrated -- https://master.php.net/note/delete/79409/integrated Del: useless -- https://master.php.net/note/delete/79409/useless Del: bad code -- https://master.php.net/note/delete/79409/bad+code Del: spam -- https://master.php.net/note/delete/79409/spam Del: non-english -- https://master.php.net/note/delete/79409/non-english Del: in docs -- https://master.php.net/note/delete/79409/in+docs Del: other reasons-- https://master.php.net/note/delete/79409 Reject -- https://master.php.net/note/reject/79409 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#132566) next »