I get this specific string from a Mysql DB:
$string = "john('[email protected]'), frank('[email protected]'),simon('[email protected]')";
I need to have the string inserted in the following code:
$sendSmtpEmail = new \SendinBlue\Client\Model\SendSmtpEmail([
'subject' => 'Report',
'sender' => ['name' => 'sender', 'email' => '[email protected]'],
'to' => [
['name' => 'john', 'email' => '[email protected]'],
['name' => 'frank', 'email' => '[email protected]'],
['name' => 'frank', 'email' => '[email protected]']
],
'htmlContent' => $output
]);
Obviously, I need the 2d array containing associative rows in $sendSmtpEmail
, but how do I create it?
CodePudding user response:
I am a bit late to answer but writing to help someone who came here searching. Hopefully this will help someone.
// String we are getting from the database
$string="john('[email protected]'), frank('[email protected]'),simon('[email protected]')";
//First explode it with comma seprated to get an array from string like
$string_exploded = explode(',', $string);
/*
array
(
[0] => john('[email protected]')
[1] => frank('[email protected]')
[2] => simon('[email protected]')
)
*/
// Loop each index to and than remove the last => ') using str_replace() function to get value as john('[email protected]') to john('[email protected]
foreach($string_exploded as $singleIndex){
// Loop each index to and than remove the last => ') using str_replace() function to get value as john('[email protected]') to john('[email protected]
$singleIndexParse = str_replace("')","", $singleIndex);
// Again explode the each index string value with => (' to get an array like
$arrayExplodedByBracket = explode("('",$singleIndexParse);
/*
array(
[0] => john
[1] => [email protected]
)
*/
// Make an array with the name and email to pass for $to array
$to[] = array(
"name"=>$arrayExplodedByBracket[0],
"email"=>$arrayExplodedByBracket[1]
);
}
// Final You will get the $to array like
/*
array
(
[0] => Array
(
[name] => john
[email] => [email protected]
)
[1] => Array
(
[name] => frank
[email] => [email protected]
)
[2] => Array
(
[name] => simon
[email] => [email protected]
)
)
*/
//Print the array in pretty format
echo "<pre>";
print_r($to);
echo "</pre>";
die();
Complete script that will do is below down for copy
// String we are getting from the database
$string="john('[email protected]'), frank('[email protected]'),simon('[email protected]')";
//First explode it with comma seprated to get an array from string like
$string_exploded = explode(',', $string);
// Loop each index to and than remove the last => ') using str_replace() function to get value as john('[email protected]') to john('[email protected]
foreach($string_exploded as $singleIndex){
// Loop each index to and than remove the last => ') using str_replace() function to get value as john('[email protected]') to john('[email protected]
$singleIndexParse = str_replace("')","", $singleIndex);
// Again explode the each index string value with => (' to get an array like
$arrayExplodedByBracket = explode("('",$singleIndexParse);
// Make an array with the name and email to pass for $to array
$to[] = array(
"name"=>$arrayExplodedByBracket[0],
"email"=>$arrayExplodedByBracket[1]
);
}
CodePudding user response:
Use preg_match_all()
to directly match one or more sequences of your formatted substrings. Use two capture groups, then map those data points to associative rows.
Code: (Demo)
$recipients = "john('[email protected]'), frank('[email protected]'),simon('[email protected]')";
preg_match_all("/(?:, ?)?([^(] )\('([^'] )'\)/", $recipients, $matches, PREG_SET_ORDER);
$sendSmtpEmail = new \SendinBlue\Client\Model\SendSmtpEmail([
'subject' => 'Report',
'sender' => ['name' => 'sender', 'email' => '[email protected]'],
'to' => array_map(
fn($row) => ['name' => $row[1], 'email' => $row[2]],
$matches
),
'htmlContent' => $output
]);