tags:

views:

255

answers:

6

Hello Guys,

i'm not very firm with regular Expressions, so i have to ask you:

How to find out with PHP if a string contains a word starting with @ ??

e.g. i have a string like "This is for @codeworxx" ???

I'm so sorry, but i have NO starting point for that :(

Hope you can help.

Thanks, Sascha

A: 

Assuming you define a word a sequence of letters with no white spaces between them, then this should be a good starting point for you:

$subject = "This is for @codeworxx";
$pattern = '/\s*@(.+?)\s/';
preg_match($pattern, $subject, $matches);
print_r($matches);

Explanation: \s*@(.+?)\s - look for anything starting with @, group all the following letters, numbers, and anything which is not a whitespace (space, tab, newline), till the closest whitespace.

See the output of the $matches array for accessing the inner groups and the regex results.

Am
Acutally that doesn't match something **starting** with @, it catches [email protected] too.
kemp
okay i will try - thanks :)
codeworxx
@kemp: tnx, fixed
Am
kemp is right... so what to do???
codeworxx
I fixed it, should work now. You can also use @MYYN's solution
Am
+3  A: 

Match anything with has some whitespace in front of a @ followed by something else than whitespace:

$ cat 1812901.php

<?php
    echo preg_match("/\B@[^\B]+/", "This should @match it");
    echo preg_match("/\B@[^\B]+/", "This should not@ match");
    echo preg_match("/\B@[^\B]+/", "This should match nothing and return 0");
    echo "\n";
?>

$ php 1812901.php 
100
The MYYN
You should change the _*_ to _+_, so it won't catch empty words
Am
thx, corrected ..
The MYYN
Also depends on what is a word, you might want to use `\w` instead of `[^\B]`
kemp
A: 

break your string up like this:

$string = 'simple sentence with five words';
$words = explode(' ', $string );

Then you can loop trough the array and check if the first character of each word equals "@":

if ($stringInTheArray[0] == "@")
Pieter888
This is actually a bad way of doing it. It's costly for nothing. Regular expressions have been created in such task in mind.
Andrew Moore
what is bad and what is bad? is not up to you to decide. this is a perfectly normal and legible way to do what OP wants, without cluttering your code with regex.
@levislevis85. Relatively speaking, keeping performance and manageability in mind, a simple regular expression *is* usually preferable to manual string manipulation, especially on large strings, even though it is a little harder to learn... And how is cluttering your code up with string manipulation code any better than cluttering it up with regexp? At least they are generally pretty short ;-)
Atli
@Atli, because with regex, you are just like reading an essay that is encoded in numbers. Too much of it makes your code hard to read. Not every solution need a regex.
A: 

okay thanks for the results - but i did a mistake - how to implement in eregi_replace ???

$text = eregi_replace('/\B@[^\B]+/','<a href="\\1">\\1</a>', $text);

does not work??!?

why? do i not have to enter the same expression as pattern?

codeworxx
post this as a new qustion
Am
+1  A: 

This might be a useful resource for regex http://www.gskinner.com/RegExr/

Adam Patterson
A: 

@OP, no need regex. Just PHP string methods

$mystr='This is for @codeworxx';
$str = explode(" ",$mystr);
foreach($str as $k=>$word){
    if(substr($word,0,1)=="@"){
        print $word;
    }
}
It would probably be better just to split the string on the @ char, and make sure it is preceded with a white-space and followed by a valid character.
Atli
down voted because I didn't use regex? What a ridiculous joke!
@Atli, if i want, i can trim the variables to get the results.