PHP Explode exclude
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开发者_StackOverflow中文版 between "" characters?
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
)
Use preg_split
instead
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.
You could use str_getcsv
with one whitespace as delimiter.
Note: This only works, if you have only one character delimiters.
精彩评论