Jump to content

Preg_Match_All() php function


terungwa

Recommended Posts

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

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)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.