Naman Bhardwaj Naman Bhardwaj - 2 months ago 20
PHP Question

How to explode multiple string with quotes

I'm trying to explode multiple string like this:

$str1 = 'Apple\'s OS and the "Microsoft OS", ----- any text with quotes & symbols ---- ';
$str2 = 'Sony\'s Laptop and the "Toshiba\'s"';

$string = "".$str1.",".$str2."";
$result = explode(',',$string);

echo "Str1 : ".$result[0]."<br/>";
echo "Str2 : ".$result[1]."<br/>";


But I'm getting this output:

Str1 : Apple's OS and the "Microsoft OS"
Str2 : ----- any text with quotes & symbols ---- //This Str2 actually is the part of Str1


I want this output -->

`Str1 : Apple's OS and the "Microsoft OS, ----- any text with quotes & symbols ----
Str2 : Sony's Laptop and the "Toshiba's"`


Please help.

Answer Source

Here's a function that does that

function implodeStrings(...$array) {
    $output = [];

    foreach($array as $str) {
        $output[] = $str;
    }

    return $output;
}

You can find a working example here.