0

I get this specific string from a Mysql DB:

$string = "john('john@yahoo.com'), frank('frank@gmail.com'),simon('simon@to.com')";

I need to have the string inserted in the following code:

$sendSmtpEmail = new \SendinBlue\Client\Model\SendSmtpEmail([
   'subject' => 'Report',
   'sender' => ['name' => 'sender', 'email' => 'sender@domain.com'],
   'to' => [
            ['name' => 'john', 'email' => 'john@domain.com'],
            ['name' => 'frank', 'email' => 'frank@domain.com'],
            ['name' => 'frank', 'email' => 'simon@domain.com']
           ],
   'htmlContent' => $output
]);

Obviously, I need the 2d array containing associative rows in $sendSmtpEmail, but how do I create it?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
R_life_R
  • 786
  • 6
  • 26
  • The question is too vague to provide a useful answer. Hard to know what you mean by having the string inserted. In your to array, are you trying to get the data from your string to to fill in those sub-arrays with a name and email using the data in your string? If so, it there a reason it is stored as a string? – C Miller Dec 20 '22 at 22:49
  • First, explode() it using the comma to separate it. Then, get everything to the left of the opening bracket. Then, remove all brackets and quote marks. – ADyson Dec 20 '22 at 23:00
  • But also...don't store structured data in this silly, unstructured way to begin with. – ADyson Dec 20 '22 at 23:00
  • Somewhat related: [Parse e-mail addresses with PHP?](https://stackoverflow.com/q/7599346/2943403) – mickmackusa Jan 01 '23 at 22:02

2 Answers2

0

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('john@yahoo.com'), frank('frank@gmail.com'),simon('simon@to.com')";
//First explode it with comma seprated to get an array from string like 
$string_exploded = explode(',', $string);
/*
array
(
    [0] => john('john@yahoo.com')
    [1] =>  frank('frank@gmail.com')
    [2] => simon('simon@to.com')
)
*/

// Loop each index to and than remove the last =>  ')   using str_replace() function to get value as  john('john@yahoo.com')  to john('john@yahoo.com
foreach($string_exploded as $singleIndex){ 
    // Loop each index to and than remove the last =>  ')   using str_replace() function to get value as  john('john@yahoo.com')  to john('john@yahoo.com
    $singleIndexParse = str_replace("')","", $singleIndex);
    // Again explode the each index string value with =>  ('  to get an array like 
    $arrayExplodedByBracket = explode("('",$singleIndexParse);
    /*
        array(
            [0] => john
            [1] => john@yahoo.com
        )
    */
    // 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] => john@yahoo.com
            )

        [1] => Array
            (
                [name] =>  frank
                [email] => frank@gmail.com
            )

        [2] => Array
            (
                [name] => simon
                [email] => simon@to.com
            )

    )
    */
    //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('john@yahoo.com'), frank('frank@gmail.com'),simon('simon@to.com')";
//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('john@yahoo.com')  to john('john@yahoo.com
foreach($string_exploded as $singleIndex){ 
    // Loop each index to and than remove the last =>  ')   using str_replace() function to get value as  john('john@yahoo.com')  to john('john@yahoo.com
    $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]
    );
}
0

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('john@yahoo.com'), frank('frank@gmail.com'),simon('simon@to.com')";

preg_match_all("/(?:, ?)?([^(]+)\('([^']+)'\)/", $recipients, $matches, PREG_SET_ORDER);

$sendSmtpEmail = new \SendinBlue\Client\Model\SendSmtpEmail([
    'subject' => 'Report',
    'sender' => ['name' => 'sender', 'email' => 'sender@domain.com'],
    'to' => array_map(
        fn($row) => ['name' => $row[1], 'email' => $row[2]],
        $matches
    ),
    'htmlContent' => $output
]);
mickmackusa
  • 43,625
  • 12
  • 83
  • 136