PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Fri Sep 22, 2017 1:14 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Wed Jun 07, 2017 6:29 am 
Offline
Forum Newbie

Joined: Wed Jun 07, 2017 6:20 am
Posts: 2
Hello All,

As this is my first post and I am posting a REGEX question.

I am using a REGEX to get the last word from the string and it is working fine only if it does not have a special characters.
test02 cIuTpSUmJOeKyp6Pvc0w
Regex:\s(\w+)$
Working fine - cIuTpSUmJOeKyp6Pvc0w

It is not working if it has special characters like $,#,-,@,).
test02 cIuTpSUm#JOeKyp6Pvc0w
Regex:\s(\w+)$
Not Working -


Can someone provide the regex for fetching the last word from the string?

Looking forward for the response from the experts.

Regards
MIAJ


Top
 Profile  
 
PostPosted: Wed Jun 07, 2017 6:45 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6551
Location: WA, USA
You have to consider what a "word" is. Do you mean anything besides whitespace? Then that's what you need to put in the regex.


Top
 Profile  
 
PostPosted: Thu Jun 08, 2017 12:10 am 
Offline
Forum Newbie

Joined: Wed Jun 07, 2017 6:20 am
Posts: 2
Yes a word can have spaces before and after.
Also main concern is the word includes special characters like %,#,#,! basically all the options a password can have.


Top
 Profile  
 
PostPosted: Thu Jun 08, 2017 12:37 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6551
Location: WA, USA
Okay, so does that mean you don't understand what I said? And now you're saying there could be spaces after, which means you have to alter the regex even further because it currently doesn't allow that.


Top
 Profile  
 
PostPosted: Sat Jun 10, 2017 3:16 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13427
Location: New York, NY, US
How about just: strrchr(rtrim($str), ' ');

http://php.net/manual/en/function.strrchr.php

_________________
(#10850)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group