jaikob Posted October 4, 2009 Share Posted October 4, 2009 I'm trying to program @ notation, like twitter does. For now I'm just literally learning about regex, and have no clue on what do do to achieve what I want. I am using preg_match for now until I get my regex right. How can I parse a string and pull out all of the @username's. Ex: preg_match('regex crap', "Hello @jaikob How are you?", $matches); Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/176434-solved-parsing/ Share on other sites More sharing options...
Garethp Posted October 4, 2009 Share Posted October 4, 2009 "~@[a-zA-Z0-9]+~" Quote Link to comment https://forums.phpfreaks.com/topic/176434-solved-parsing/#findComment-930011 Share on other sites More sharing options...
ProXy_ Posted October 4, 2009 Share Posted October 4, 2009 Yup, And just incase. if you need help on how to use & display this code: <?php $string="@welcome @decide @homicide @loser doit andlaugh"; preg_match_all("~@[a-zA-Z0-9]+~", $string, $matches); foreach ($matches as $val) { for ($i=0;$i<count($val); $i++) { echo "matched: " . $val[$i] . "\n"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/176434-solved-parsing/#findComment-930013 Share on other sites More sharing options...
nrg_alpha Posted October 4, 2009 Share Posted October 4, 2009 Hmm.. using a for loop within a foreach like that? Kind of strange. You can simply tap into $matches[0] instead. Also note that you can simplify the pattern by using the 'i' modifier: $string="@welcome @decide @homicide @loser doit andlaugh"; preg_match_all('#@[a-z0-9]+#i', $string, $matches); foreach ($matches[0] as $val) { echo "matched: $val<br />\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/176434-solved-parsing/#findComment-930017 Share on other sites More sharing options...
.josh Posted October 4, 2009 Share Posted October 4, 2009 think i'd maybe wrap that in \b or \s depending on what the subject really is Quote Link to comment https://forums.phpfreaks.com/topic/176434-solved-parsing/#findComment-930018 Share on other sites More sharing options...
jaikob Posted October 4, 2009 Author Share Posted October 4, 2009 Thank you all for the help! Quote Link to comment https://forums.phpfreaks.com/topic/176434-solved-parsing/#findComment-930116 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.