|
użytkowników online: 104
|
OPINIE UŻYTKOWNIKÓW
|
Porady zamieszczone tutaj przez Darka są pomocne w wielu chwilach. Wielokrotnie tworząc jakiś złożony serwis korzystam z tych porad. Można by tworzyć samemu te skrypty, ale tak naprawdę czy nie lepiej jest wziąć skrypt z tej strony i zmodyfikowac go dla swoich potrzeb? Wprawdzie możemy taki skrypt napisać sami, ale po co, skoro stracimy czas na coś, co ktoś juz napisał, przetestował i może zagwarantować, że działa poprawnie. Któryś raz z rzędu opłacam abonament i nie raz jeszcze opłacę. Kawał dobrej roboty i ogrom wiedzy w jednym miejscu.
Piotr Karamański Design Studio
|
|
PODRĘCZNIK PHP 5.x, 4.x, 3.x - częściowo spolszczony / źródło: www.php.net
[Spis]
[A]
[B]
[C]
[D]
[E]
[F]
[G]
[H]
[I]
[J]
[K]
[L]
[M]
[N]
[O]
[P]
[Q]
[R]
[S]
[T]
[U]
[V]
[X]
[W]
[Z]
strrpos (PHP 3, PHP 4, PHP 5) strrpos --
Find position of last occurrence of a char in a string
Descriptionint strrpos ( string haystack, string needle [, int offset] )
Returns the numeric position of the last occurrence of
needle in the
haystack string. Note that the needle in
this case can only be a single character in PHP 4. If a string is passed
as the needle, then only the first character of that string will
be used.
If needle is not found, returns FALSE.
It is easy to mistake the return values for "character found at
position 0" and "character not found". Here's how to detect
the difference:
If needle is not a string, it is converted
to an integer and applied as the ordinal value of a character.
Notatka:
As of PHP 5.0.0 offset may
be specified to begin searching an arbitrary number of characters into
the string. Negative values will stop searching at an arbitrary point
prior to the end of the string.
Notatka:
The needle may be a string of more than one
character as of PHP 5.0.0.
See also strpos(),
strripos(),
strrchr(),
substr(),
stristr(), and
strstr().
User Contributed Notesnh_handyman
22-Sep-2005 12:59
As noted in some examples below, strrpos does not act the same on every platform!
On Linux, it returns the position of the end of the target
On Windows, it returns the position of the start of the target
strrpos ("c:/somecity/html/t.php")
returns 11 on Windows
returns 16 on Linux
Brian
gordon at kanazawa-gu dot ac dot jp
14-Sep-2005 06:56
The "find-last-occurrence-of-a-string" functions suggested here do not allow for a starting offset, so here's one, tried and tested, that does:
function my_strrpos($haystack, $needle, $offset=0) {
// same as strrpos, except $needle can be a string
$strrpos = false;
if (is_string($haystack) && is_string($needle) && is_numeric($offset)) {
$strlen = strlen($haystack);
$strpos = strpos(strrev(substr($haystack, $offset)), strrev($needle));
if (is_numeric($strpos)) {
$strrpos = $strlen - $strpos - strlen($needle);
}
}
return $strrpos;
}
genetically altered mastermind at gmail
22-Aug-2005 07:30
Very handy to get a file extension:
$this->data['extension'] = substr($this->data['name'],strrpos($this->data['name'],'.')+1);
fab
10-Aug-2005 01:07
RE: hao2lian
There are a lot of alternative - and unfortunately buggy - implementations of strrpos() (or last_index_of as it was called) on this page. This one is a slight modifiaction of the one below, but it should world like a *real* strrpos(), because it returns false if there is no needle in the haystack.
<?php
function my_strrpos($haystack, $needle) {
$index = strpos(strrev($haystack), strrev($needle));
if($index === false) {
return false;
}
$index = strlen($haystack) - strlen($needle) - $index;
return $index;
}
?>
lwoods
06-Aug-2005 09:03
If you are a VBScript programmer ("ex-" of course), you will find that 'strrpos' doesn't work like the VBScript 'instrRev' function.
Here is the equivalent function:
VBScript:
k=instrrev(s,">",j);
PHP Equivalent of the above VBScript:
$k=strrpos(substr($s,0,$j),'>');
Comments:
You might think (I did!) that the following PHP function call would be the equivant of the above VBScript call:
$kk=strrpos($s,'>',$j);
NOPE! In the above PHP call, $j defines the position in the string that should be considered the BEGINNING of the string, whereas in the VBScript call, j is to be considered the END of the string, as far as this search is concerned. Anyway, the above 'strrpos' with the 'substr' will work.
(Probably faster to write a for loop!)
hao2lian
03-Aug-2005 04:50
Yet another correction on the last_index_of function algorithm:
function last_index_of($haystack, $needle) {
$index = strpos(strrev($haystack), strrev($needle));
$index = strlen($haystack) - strlen($needle) - $index;
return $index;
}
"strlen(index)" in the most recent one should be "strlen($needle)".
jonas at jonasbjork dot net
06-Apr-2005 10:25
I needed to remove last directory from an path, and came up with this solution:
<?php
$path_dir = "/my/sweet/home/";
$path_up = substr( $path_dir, 0, strrpos( $path_dir, '/', -2 ) )."/";
echo $path_up;
?>
Might be helpful for someone..
08-Mar-2005 08:14
In the below example, it should be substr, not strrpos.
<PHP?
$filename = substr($url, strrpos($url, '/') + 1);
?>
escii at hotmail dot com ( Brendan )
11-Jan-2005 04:12
I was immediatley pissed when i found the behaviour of strrpos ( shouldnt it be called charrpos ?) the way it is, so i made my own implement to search for strings.
<?
function proper_strrpos($haystack,$needle){
while($ret = strrpos($haystack,$needle))
{
if(strncmp(substr($haystack,$ret,strlen($needle)),
$needle,strlen($needle)) == 0 )
return $ret;
$haystack = substr($haystack,0,$ret -1 );
}
return $ret;
}
?>
griffioen at justdesign dot nl
17-Nov-2004 07:57
If you wish to look for the last occurrence of a STRING in a string (instead of a single character) and don't have mb_strrpos working, try this:
function lastIndexOf($haystack, $needle) {
$index = strpos(strrev($haystack), strrev($needle));
$index = strlen($haystack) - strlen(index) - $index;
return $index;
}
nexman at playoutloud dot net
07-Oct-2004 06:22
Function like the 5.0 version of strrpos for 4.x.
This will return the *last* occurence of a string within a string.
function strepos($haystack, $needle, $offset=0) {
$pos_rule = ($offset<0)?strlen($haystack)+($offset-1):$offset;
$last_pos = false; $first_run = true;
do {
$pos=strpos($haystack, $needle, (intval($last_pos)+(($first_run)?0:strlen($needle))));
if ($pos!==false && (($offset<0 && $pos <= $pos_rule)||$offset >= 0)) {
$last_pos = $pos;
} else { break; }
$first_run = false;
} while ($pos !== false);
if ($offset>0 && $last_pos<$pos_rule) { $last_pos = false; }
return $last_pos;
}
If my math is off, please feel free to correct.
- A positive offset will be the minimum character index position of the first character allowed.
- A negative offset will be subtracted from the total length and the position directly before will be the maximum index of the first character being searched.
returns the character index ( 0+ ) of the last occurence of the needle.
* boolean FALSE will return no matches within the haystack, or outside boundries specified by the offset.
harlequin AT gmx DOT de
26-May-2004 06:59
this is my function for finding a filename in a URL:
<?php
function getfname($url){
$pos = strrpos($url, "/");
if ($pos === false) {
return false;
} else {
$len = strlen($url);
if ($len < $pos){
print "$len / $pos";
return false;
} else {
$filename = substr($url, $pos+1, $len-$pos-1);
}
}
return $filename;
}
?>
tsa at medicine dot wisc dot edu
25-May-2004 02:17
What the heck, I thought I'd throw another function in the mix. It's not pretty but the following function counts backwards from your starting point and tells you the last occurrance of a mixed char string:
<?php
function strrposmixed ($haystack, $needle, $start=0) {
if($start == 0) {
$start = strlen($haystack);
}
$currentStrPos=$start;
$lastFoundPos=false;
while($currentStrPos != 0) {
if(!(strpos($haystack,$needle,$currentStrPos) === false)) {
$lastFoundPos=strpos($haystack,$needle,$currentStrPos);
break;
}
$currentStrPos--;
}
if($lastFoundPos === false) {
return false;
} else {
return $lastFoundPos;
}
}
?>
dreamclub2000 at hotmail dot com
04-Feb-2004 09:17
This function does what strrpos would if it handled multi-character strings:
<?php
function getLastStr($hay, $need){
$getLastStr = 0;
$pos = strpos($hay, $need);
if (is_int ($pos)){ while($pos) {
$getLastStr = $getLastStr + $pos + strlen($need);
$hay = substr ($hay , $pos + strlen($need));
$pos = strpos($hay, $need);
}
return $getLastStr - strlen($need);
} else {
return -1;
|