terungwa Posted April 8, 2014 Share Posted April 8, 2014 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. Link to comment https://forums.phpfreaks.com/topic/287621-preg_match_all-php-function/ Share on other sites More sharing options...
DavidAM Posted April 9, 2014 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) 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
Archived
This topic is now archived and is closed to further replies.