terungwa Posted April 8, 2014 Share Posted April 8, 2014 (edited) I am trying to extract the strings that match this pattern @xyz from an input string using the preg_match_all() php function. My results are to be outputted to $match, as an array where $match[0] should contain all full matches. But what I am getting is an empty array. I would appreciate a clue to resolve this. My code is below. <?php $data = "@mike The party will start at 10:30 pm and @John run untill 12:30 am. Please @Emeka, could you inform @Joseph?"; preg_match_all('/^@(a-z)*$/', $data, $match, PREG_PATTERN_ORDER); echo "Matches: <br>"; print_r($match[0]); ?> Thanks. Edited April 8, 2014 by terungwa Quote Link to comment https://forums.phpfreaks.com/topic/287621-preg_match_all-php-function/ Share on other sites More sharing options...
Solution DavidAM Posted April 9, 2014 Solution Share Posted April 9, 2014 The caret ("^") at the beginning, anchors the match to the beginning of the input string; The dollar ("$") at the end, anchors the match to the end of the input string; The parenthesis ("( )") define a capture group, not a range of characters; So try '/@[a-z]+/i' The "i" after the expression makes it case-insensitive. The "+" means one or more (the askterisk, "*", says zero or more which means an "@" alone would match). Also, this will not allow digits in the token, nor special characters, only the letters "A" - "Z" (upper- or lower- case) Quote Link to comment https://forums.phpfreaks.com/topic/287621-preg_match_all-php-function/#findComment-1475443 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.