I want to group the words starting with character $ from the text area content . This is the editor content. Example paragrraph:
Hi $firstname, Please find the company credentials mentioned below: Employee Name: Employee ID: Employee username: $username Email ID: $email Password: $password Regards, Administration Team
My Code:
$pattern = '/([$])\w+/';
preg_match($pattern, $input, $matches);
print_r($matches);
Output is :
Array (
[0] => $firstname
[1] => $
)
I need the output to be:
Array (
[0] => $firstname
[1] => $username
[2]=>$email
[3]=>$password
)
What am I doing wrong?
$matches = array();
preg_match_all('/\$\w+/', $text, $matches);
print_r($matches);
You need to use preg_match_all
. preg_match
returns only first match. Also, fix your regexp to match the actual text:
$pattern = '/[$](\w+)/';
preg_match_all($pattern, $input, $matches);
foreach($matches as $match) {
echo $match[0] . ': ' . $match[1];
}
This will output:
$firstname : firstname
$username : username
$email : email
$password : password