PHP在等号之前获取文本作为数组键并在作为数组值之后

As an example below there is a content contains multiple equals. How the PHP function should be able to parse all equals as an array with the key is text before equal sign and the value is after it?

Lorem ipsum id="the id" dolor sit amet, consectetur name="the name" adipisicing elit, sed do type="the type" eiusmod tempor incididunt ut labore et dolore magna aliqua.

And as the result will be like this:

Array ( 
    [id]   => the id
    [name] => the name
    [type] => the type
)

I would use preg_match_all to catch all of those instances in that string.

preg_match_all('/([^\s]*?)="([^"]*?)"/',$text, $matches);

Would find the variables that you want and set them in two arrays: $matches[1] and $matches[2]. You can then put them into a new array if you want with a for or foreach loop.

I've made an example in codepad, if you want to look at it, here.

$string; // This is the string you already have.

$matches = array(); // This will be the array of matched strings.

preg_match_all('/\s[^=]+="[^"]+"/', $string, $matches);

$returnArray = array();
foreach ($matches as $match) { // Check through each match.
    $results = explode('=', $match); // Separate the string into key and value by '=' as delimiter.
    $returnArray[$results[0]] = trim($results[1], '"'); // Load key and value into array.
}
print_r($returnArray);