Current location: Hot Scripts Forums » Programming Languages » PHP » what is the best method to find last word in variable ?


what is the best method to find last word in variable ?

Reply
  #1 (permalink)  
Old 09-13-04, 12:45 PM
GS300 GS300 is offline
Newbie Coder
 
Join Date: May 2004
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
what is the best method to find last word in variable ?

Hi Guys ,
I have a vriable contain text with html tags
I want to cut the first 200 character from this variable without counting html tags .
What is the best method to do that ?

I suggest the folwing method :
1- move the text to another variable with strip_tags function .
2- get the first 200 character from new variable and put it in $mirror_var .
3- find the last word in $mirror_var .
4- get the the characters from main variable upto lastword .

But I have a problem in finding last word in $mirror_var
I want to know what is the best method to find last word in variable contain long text ?
OR
if there is another method better than my method

Cheers

Last edited by GS300; 09-13-04 at 12:47 PM.
Reply With Quote
  #2 (permalink)  
Old 09-13-04, 01:13 PM
<?Wille?> <?Wille?> is offline
Junior Code Guru
 
Join Date: Jan 2004
Location: Helsinki, Finland
Posts: 666
Thanks: 0
Thanked 0 Times in 0 Posts
to get the last word (untested)

PHP Code:

$mirror_array explode(' '$mirror_var);

$mirror_num count($mirror_array);
$n mirror_num -1;
$lastword $mirror_array[$n]; 
dont know how fast this is but it should give you the last word (assuming words are separated with space (' '))
hope it helps
Wille

Last edited by <?Wille?>; 09-13-04 at 01:17 PM. Reason: wrong function
Reply With Quote
  #3 (permalink)  
Old 09-13-04, 01:15 PM
Eclipse's Avatar
Eclipse Eclipse is offline
Coding Addict
 
Join Date: May 2004
Location: Long Island, New York
Posts: 356
Thanks: 0
Thanked 0 Times in 0 Posts
Set up the whole text as a var ie $text then:
PHP Code:

$data explode(' '$text);

$data_c count($data);
$last_word $data[$data_c];
echo(
$last_word); //or what ever you want to do with the last word 
Well hope that works...
Reply With Quote
  #4 (permalink)  
Old 09-14-04, 03:01 PM
mikaelf mikaelf is offline
Wannabe Coder
 
Join Date: Jun 2004
Location: php[dot]net
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
Did you mean:
PHP Code:

preg_match("/(?<=\040)([\w\._-]*?)$/",$text,$matches);

echo 
$matches[0]; 
__________________
Useful PHP links:
bugs.php.net - for reporting PHP bugs
pear.php.net - PHP extension and application repository
pecl.php.net - get non standard PHP modules, submit yours
www.phpclasses.org - PHP classes repository
Reply With Quote
  #5 (permalink)  
Old 09-15-04, 07:13 PM
GS300 GS300 is offline
Newbie Coder
 
Join Date: May 2004
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks all guys

I tried all methods and found :

<?Wille?> : your method is good but it will fail in some cases

Eclipse : I think we must decrement $data_c to make it work .

mikaelf : your method didn't work may be I didn't understand it .

I found a good method which is :

PHP Code:

$txt='your text with words'
$out=preg_split('/\s+/',trim($txt)); 
echo 
$out[count($out)-1]; 
and it will work in all cases .
Reply With Quote
  #6 (permalink)  
Old 09-15-04, 08:35 PM
Eclipse's Avatar
Eclipse Eclipse is offline
Coding Addict
 
Join Date: May 2004
Location: Long Island, New York
Posts: 356
Thanks: 0
Thanked 0 Times in 0 Posts
Yea i found that out and was going to post that now but I saw that you got it.
Reply With Quote
  #7 (permalink)  
Old 09-15-04, 09:13 PM
mikaelf mikaelf is offline
Wannabe Coder
 
Join Date: Jun 2004
Location: php[dot]net
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by GS300
mikaelf : your method didn't work may be I didn't understand it .

I found a good method which is :

PHP Code:

$txt='your text with words'
$out=preg_split('/\s+/',trim($txt)); 
echo 
$out[count($out)-1]; 
and it will work in all cases .
Hi GS300,
The regex I wrote should run if you test in in normal condition. If you want a faster regex which can satisfy all condition you can give a try to this one:
PHP Code:

$txt="your text with words. try to find the last word using several spaces before the last word    gsvdfg5412f_tw4t=agast3)(@35.\n";

preg_match("/(?<=\040)([^\s]+?)$/",trim($txt),$matches);
echo 
$matches[0]; 
How the above code works:
1. It will assert for a single space from the end of the string.
2. The word after the space is the last word. (simple enough, huh?)
3. It will yield only 1 match.

Comparation with your preg_split (how it works):
1. It will explode the string by empty spaces (space, tab)
2. It will yield an array with n elements.
3. The last word is the (n-1)th element of the array.

Using preg_match, you can save several "precious" microsecond and less memory consumption. Anyway, it's worth nothing compared to whole system.
__________________
Useful PHP links:
bugs.php.net - for reporting PHP bugs
pear.php.net - PHP extension and application repository
pecl.php.net - get non standard PHP modules, submit yours
www.phpclasses.org - PHP classes repository
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP variable sharad PHP 2 09-10-04 02:06 PM
refreshing php queries jaspan PHP 13 06-21-04 03:18 PM
one main method sniperx Everything Java 6 01-23-04 06:02 AM
how to find control in placeholder nnet ASP.NET 0 01-13-04 11:44 PM
Call a variable name dynamically Kenrette ASP 2 11-12-03 09:48 AM


All times are GMT -5. The time now is 04:29 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.