I extracted two inputed usernames from a text string with @ mention tag.
Now I want to put them in foreach loop so I can use my doctrine instance to search for their user name.
My code:
JavaScript
x
function find(TextEntity $text)
{
preg_match_all('/@([a-zA-Z0-9]+)/', $text->getText(), $usernames);
$extractedText = implode('", "', $usernames[1]);
}
The result I get:
“john”, “jane”
I can’t find a way to extract them so I can use one by one in foreach loop.
So my try is: (update: working solution)
JavaScript
$usernames = $usernames[1];
$query = $this->em->getRepository(User::class)
->createQueryBuilder('m')
->select('m')
->where('m.email IN (:emails)')
->setParameter('emails', $usernames)
->getQuery()
->getResult();
Advertisement
Answer
As usernames[1]
is already an array you can iterate over it:
JavaScript
preg_match_all('/@([a-zA-Z0-9]+)/', $text->getText(), $usernames);
$usernames = $usernames[1];
And as doctrine supports where in ()
clause in findBy
you can use this array without foreach
:
JavaScript
$findUsers = $this->em->getRepository(User::class)->findBy([
'username' => $usernames
]);
print_r(findUsers);
You can modify this code as needed.