php阵列分裂

  • I am passing the space separated value to in input and split this in operation and make an array.

givecard 442 ashutosh2 Y

  • But i want to pass the message in this string and want to split as complete message in operation or output.

givecard 442 ashutosh2 Y hi how are you

if i split this line than i get the result is

Array
(
    [0] => givecard
    [1] => 442
    [2] => ashutosh2
    [3] => Y
    [4] => hi
    [5] => how
    [6] => are
    [7] => you
)

But i want like this

 Array
    (
        [0] => givecard
        [1] => 442
        [2] => ashutosh2
        [3] => Y
        [4] => hi how are you 
    )

as here hi how are you i want to extract as complete one message in a variable . How can i achieve this

Edit:

preg_split('/\s+/', "givecard 442 ashutosh2 Y hi how are you", 5);

Say you have a function like so:

function operate_on_string($string) {
    $out = preg_split('/\s+/', $string);

    # ...
    return $out;
}
  • Option one:

Pass the string "givecard 442 ashutosh2 Y" and have "hi how are you" appended to the end of it:

operate_on_string("givecard 442 ashutosh2 Y" . ' ' . "hi how are you");
  • Option two:

Just pass the first string to it, then append the rest to the output:

array_merge(operate_on_string("givecard 442 ashutosh2 Y"), preg_split('/\s+/', "hi how are you"));

From your problem, I guess it's a preformatted string. Let me see, can you use explode():

$format = explode(" ", $data, 5);

Hm, is it possible to separate values by smt else? For example:

givecard:442:ashutosh2:Y:hi how are you

and then explode it on :