note 68519 added to function.strrchr
| From: | repleyatfreemaildotit at osu1 dot php dot net | Date: | Mon, 31 Jul 2006 13:43:52 +0000 |
| Subject: | note 68519 added to function.strrchr | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-115376@lists.php.net to get a copy of this message | ||
Style-upgrade of readless's bugfix:
a more elegant solution is to use '!=='.
Regards from Italy.
<?
//strxchr(string haystack, string needle [, bool int leftinclusive [, bool int rightinclusive ]])
function strxchr($haystack, $needle, $l_inclusive = 0, $r_inclusive = 0){
// Note our use of !==. Now work if the position of $needle in $haystack was the 0th (first)
character.
if(strrpos($haystack, $needle) !== false){
//Everything before last $needle in $haystack.
$left = substr($haystack, 0, strrpos($haystack, $needle) + $l_inclusive);
//Switch value of $r_inclusive from 0 to 1 and viceversa.
$r_inclusive = ($r_inclusive == 0) ? 1 : 0;
//Everything after last $needle in $haystack.
$right = substr(strrchr($haystack, $needle), $r_inclusive);
//Return $left and $right into an array.
$a = array($left, $right);
return $a;
}else{
return false;
}
}
//start test
$haystack_array = array("....unknown.txt.log.......gif.....jpeg", "/index.html",
"index.html", "http://www.example.com/", "/folder_one/",
"/folder_one/folder_two/", "folder_two/");
$needle_array = array(".", "/", ".", "/", "/",
"/", "/");
$count_haystack = count($haystack_array);
$count_needle = count($needle_array);
if($count_haystack == $count_needle){
$l_inclusive = 1; //1 for inclusive mode, 0 for NOT inclusive mode
$r_inclusive = 1; //1 for inclusive mode, 0 for NOT inclusive mode
for($pointer = 0; $pointer < $count_haystack; $pointer++) {
$haystack = $haystack_array[$pointer];
$needle = $needle_array[$pointer];
$a = strxchr($haystack, $needle, $l_inclusive, $r_inclusive);
if($a){echo "Left: <strong>" . $a[0] . "</strong><br /> Right:
<strong>" . $a[1] . "</strong><br /><br />";}else{echo
"Failed!<br />";}
}
}else{
echo 'Count of "$haystack_array" and count of "$needle_array" are not
equal!';
}
//end test
?>
Thanks to all.
Repley
----
Server IP: 213.92.11.19
Probable Submitter: 84.222.146.145
----
X-Spam-Status: No, hits=3.3 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER,
HTML_MESSAGE autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.strrchr.php
Edit -- https://master.php.net/note/edit/68519
Del: integrated -- https://master.php.net/note/delete/68519/integrated
Del: useless -- https://master.php.net/note/delete/68519/useless
Del: bad code -- https://master.php.net/note/delete/68519/bad+code
Del: spam -- https://master.php.net/note/delete/68519/spam
Del: non-english -- https://master.php.net/note/delete/68519/non-english
Del: in docs -- https://master.php.net/note/delete/68519/in+docs
Del: other reasons-- https://master.php.net/note/delete/68519
Reject -- https://master.php.net/note/reject/68519
Search -- https://master.php.net/manage/user-notes.php