-2

I have a string with spaces as follows.

$name = 'name="shadow" msg="Your message"';

I want to explode the string such as to get name="shadow" and msg="Your message". But the message inside msg is also splitted

On exploding using whitespace i am not getting desired output

$code = explode(' ', $name);

I want to get

$code[0] = 'name="shadow"'
$code[1] = 'msg="Your message"'

But i get this

$code[0] = 'name="shadow"'
$code[1] = 'msg="Your'
$code[3] = 'message"'
Mohammad
  • 21,175
  • 15
  • 55
  • 84
Shadow
  • 141
  • 1
  • 7

1 Answers1

2

Use /(?<=\")\s/ regex in preg_split(). The regex select any space after " character

$name = 'name="shadow" msg="Your message"';
$code = preg_split('/(?<=\")\s/', $name);

Check result in demo

Mohammad
  • 21,175
  • 15
  • 55
  • 84