note 103191 added to function.strrchr
| From: | matthewkastor at live dot com | Date: | Wed, 30 Mar 2011 21:07:24 +0000 |
| Subject: | note 103191 added to function.strrchr | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-178362@lists.php.net to get a copy of this message | ||
<?php
/**
* Removes the preceeding or proceeding portion of a string
* relative to the last occurrence of the specified character.
* The character selected may be retained or discarded.
*
* Example usage:
* <code>
* $example = 'http://somesite.com/path/file.php';
* $cwd_relative[] = cut_string_using_last('/', $example, 'left', true);
* $cwd_relative[] = cut_string_using_last('/', $example, 'left', false);
* $cwd_relative[] = cut_string_using_last('/', $example, 'right', true);
* $cwd_relative[] = cut_string_using_last('/', $example, 'right', false);
* foreach($cwd_relative as $string) {
* echo "$string <br>".PHP_EOL;
* }
* </code>
*
* Outputs:
* <code>
* http://somesite.com/path/
* http://somesite.com/path
* /file.php
* file.php
* </code>
*
* @param string $character the character to search for.
* @param string $string the string to search through.
* @param string $side determines whether text to the left or the right of the character is
returned.
* Options are: left, or right.
* @param bool $keep_character determines whether or not to keep the character.
* Options are: true, or false.
* @return string
*/
function cut_string_using_last($character, $string, $side, $keep_character=true) {
$offset = ($keep_character ? 1 : 0);
$whole_length = strlen($string);
$right_length = (strlen(strrchr($string, $character)) - 1);
$left_length = ($whole_length - $right_length - 1);
switch($side) {
case 'left':
$piece = substr($string, 0, ($left_length + $offset));
break;
case 'right':
$start = (0 - ($right_length + $offset));
$piece = substr($string, $start);
break;
default:
$piece = false;
break;
}
return($piece);
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 71.205.32.227
----
Manual Page -- http://www.php.net/manual/en/function.strrchr.php
Edit -- https://master.php.net/note/edit/103191
Del: integrated -- https://master.php.net/note/delete/103191/integrated
Del: useless -- https://master.php.net/note/delete/103191/useless
Del: bad code -- https://master.php.net/note/delete/103191/bad+code
Del: spam -- https://master.php.net/note/delete/103191/spam
Del: non-english -- https://master.php.net/note/delete/103191/non-english
Del: in docs -- https://master.php.net/note/delete/103191/in+docs
Del: other reasons-- https://master.php.net/note/delete/103191
Reject -- https://master.php.net/note/reject/103191
Search -- https://master.php.net/manage/user-notes.php