GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 17-Feb-2007, 17:35
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough

Find the last occurrence of a string in a string


I am in need of a simple PHP script to find the position of the LAST occurrence of a string, within a string.

For example, if I was searching for the last me in the following string, I would expect the script to return 30:

Code:
.123456789.123456789.123456789.12 ------^--------------------^--^-- The same thing happened to meemem
  #2  
Old 17-Feb-2007, 19:13
TurboPT's Avatar
TurboPT TurboPT is offline
Regular Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 966
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Find the last occurrence of a string in a string


How about the strrpos() function?

(tag won't keep the newline [before 'echo'] for some reason)
PHP Code:

$yourString = "The same thing happened to meemem";

echo strrpos($yourString, "me"); 


It gave me 30.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #3  
Old 18-Feb-2007, 04:06
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough

Re: Find the last occurrence of a string in a string


Quote:
Note that the needle in this case can only be a single character in PHP 4

I am sorry, but I should have mentioned it already -- I am using PHP 4.
  #4  
Old 18-Feb-2007, 08:47
TurboPT's Avatar
TurboPT TurboPT is offline
Regular Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 966
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Find the last occurrence of a string in a string


Ah ha, I missed that line in the manual...

Ok then, taking an example from the last post on that page, slightly modified, how about this:
PHP Code:

$haystack = "The same thing happened to meemem";
$needle = "me";

echo strlen($haystack) - strpos(strrev($haystack), strrev($needle)) - strlen($needle); 


Gives 30. (the computation of: 33 - 1 - 2)
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #5  
Old 18-Feb-2007, 08:53
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough

Re: Find the last occurrence of a string in a string


Good one! Thank you.

I am doing more than one thing at this time, so excuse me for not going through the comments (on that page) sooner.
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Read a .html file, check that file for links salemite C Programming Language 10 17-Jan-2008 07:56
Message Class TransformedBG C++ Forum 5 29-Nov-2006 21:28
Help wit my source code compiler errors Krandygrl00 C++ Forum 1 06-Jun-2005 08:14
Converting string to float CT++ C++ Forum 2 10-May-2005 11:29

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 09:56.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.