-
Mar 26th, 2005, 02:26 AM
#1
Find last occurrence of a string in a string [RESOLVED]
I do not have PHP 5, so I cannot use strrpos().
Does anyone have a code snippet to find the last occurrence of a string in a string for pre-PHP5?
-
Mar 26th, 2005, 04:27 AM
#2
Re: Find last occurrence of a string in a string
You could use the strrpos() function.
-
Mar 26th, 2005, 11:03 PM
#3
Re: Find last occurrence of a string in a string
strrpos -- Find position of last occurrence of a char in a string
And,
The needle may be a string of more than one character as of PHP 5.0.0.
So I can't use that.
What I ended up doing though, was:
PHP Code:
$largestring = //whatever
$reverselarge = strrev($largestring);
$posoflastchevron = strlen($largestring) - strpos($reverselarge, ";781#;pma&");
Although I don't quite like this method.
-
Mar 26th, 2005, 11:06 PM
#4
Re: Find last occurrence of a string in a string
Hmm... just saw this:
PHP Code:
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;
}
-
Mar 27th, 2005, 04:18 AM
#5
Re: Find last occurrence of a string in a string
Sorry - I didn't read your question.
Although your solution need only be a few lines. This works fine
PHP Code:
function strrpos_new($haystack, $needle, $offset = 0)
{
/* set initial return value to false, just incase the string
is never found */
$old_pos = false;
/* use strpos() until no match is found */
while (($pos = strpos($haystack, $needle, $offset)) !== false) {
$old_pos = $pos;
$offset = $old_pos + strlen($needle);
}
/* return the old position */
return $old_pos;
}
-
Mar 27th, 2005, 05:44 AM
#6
Re: Find last occurrence of a string in a string
And this version can handle negative offsets
PHP Code:
function strrpos_new($haystack, $needle, $offset = 0)
{
/* check for a negative offset and assertain where
searching should stop */
if ($offset < 0) {
$stop = strlen($haystack) + $offset - 1;
$offset = 0;
} else {
$stop = strlen($haystack) -1;
}
/* set initial return value to false, just incase the string
is never found */
$old_pos = false;
/* use strpos() until no match is found */
while (($pos = strpos($haystack, $needle, $offset)) !== false
&& $pos <= $stop) {
$old_pos = $pos;
$offset = $old_pos + strlen($needle);
}
/* return the old position */
return $old_pos;
}
-
Mar 27th, 2005, 12:45 PM
#7
Re: Find last occurrence of a string in a string
Be a doll and add [Resolved] to the thread title, will ya?
-
Mar 27th, 2005, 02:19 PM
#8
Re: Find last occurrence of a string in a string
I shall require a fee for completion of such a service.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|