php to find @ symbol

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

php to find @ symbol

Post by Citizen »

I need php to find any word in a string that begins with an @ symbol. It has to be by itself:

For instance:

@myname = match
asdf@myname = not a match
@ myname = not a match
@@ = not a match
@@myname = not a match

Any ideas?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Re: php to find @ symbol

Post by Burrito »

Code: Select all

 
preg_match("/(^@[^@\s]+.*?)$/",$string,$matches);
 
untested but that should match all of your cases above....
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: php to find @ symbol

Post by prometheuzz »

Citizen wrote:I need php to find any word in a string ...
You will need to define what a "word" is for you. The suggestion of the previous poster will also match strings like:

Code: Select all

"@~~~~"
"@abc,"
"@1ab2"
etc.
Post Reply