Jump to content

Preg_Match_All() php function


terungwa
Go to solution Solved by DavidAM,

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.

Edited by terungwa
Link to comment
Share on other sites

  • Solution

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.