2

I have a string with data like this:

8487613 1298296324 1a6ad892e547da07f35221fdfe6f70dd "|MoDo|" 178.211.28.126 "Battlefield Bad Company 2" "BC Stuff and name" "Violation (AIMBOT) #50246"

In my PHP I use explode like this:

$more_info = explode(' ', $banned_players);

But it separates spaces in "Battlefield Bad Company 2" and I don't want that to happen.

So is there any way for explode with spaces but not between "" characters?

4 Answers 4

4

Try str_getcsv(). It's intended for tab- or comma-separated values, but it works very well with just the space as separator:

print_r(str_getcsv('8487613 1298296324 1a6ad892e547da07f35221fdfe6f70dd "|MoDo|" 178.211.28.126 "Battlefield Bad Company 2"', " "));

Results in:

Array
(
    [0] => 8487613
    [1] => 1298296324
    [2] => 1a6ad892e547da07f35221fdfe6f70dd
    [3] => |MoDo|
    [4] => 178.211.28.126
    [5] => Battlefield Bad Company 2
    [6] => BC Stuff and name
    [7] => Violation (AIMBOT) #50246
)
Sign up to request clarification or add additional context in comments.

1 Comment

Or fgetcsv, fseek and php://temp if < 5.3.0.
1

Use preg_split instead

1 Comment

Well I'm not really good with regex and I want to finish this code quickly. ^^
0

Explode the " character, you'll have 3 arrays, then explode first array and last array with space character, and use array_push() and array_merge() to merge them all.

Comments

0

You could use str_getcsv with one whitespace as delimiter.

Note: This only works, if you have only one character delimiters.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.