Mike Casan Ballester
Mike Casan Ballester

Reputation: 1770

Split string between two special characters

The Objective

I want to retrieve all the emails between the special characters < > of the string into an array of emails

$list = '"momo rabit" <[email protected]>, "youn" <[email protected]>, "yourmail" <[email protected]>, "yovan" <[email protected]>, "popol" <[email protected]>'; 

What I tried

Based on this answer, it should work.

$list = htmlspecialchars($list);
preg_match_all("/<(.*?)>/", $list, $list_step);
var_dump($list_step); // Return empty arrays

I also tried without success

preg-split

str_getcsv

Upvotes: 1

Views: 309

Answers (1)

chris85
chris85

Reputation: 23880

The answer is pretty easy. The htmlspecialchars converts the < and > to &lt; and &gt;. So either change the regex or remove that function.

$list = '"momo rabit" <[email protected]>, "youn" <[email protected]>, "yourmail" <[email protected]>, "yovan" <[email protected]>, "popol" <[email protected]>'; 
//$list = htmlspecialchars($list);
preg_match_all("/<(.*?)>/", $list, $list_step);
var_dump($list_step); // Return empty arrays

Demo: https://3v4l.org/VUY1U

Alternative:

$list = '"momo rabit" <[email protected]>, "youn" <[email protected]>, "yourmail" <[email protected]>, "yovan" <[email protected]>, "popol" <[email protected]>'; 
$list = htmlspecialchars($list);
preg_match_all("/&lt;(.*?)&gt;/", $list, $list_step);
var_dump($list_step); // Return empty arrays

Demo: https://3v4l.org/gI07A

Upvotes: 3

Related Questions