"Empty delimiter" Warning when using PHP explode() function
Asked Answered
C

4

15

In javascript, var myStringToArray = myString.split(''); is perfectly acceptable.

But in PHP, $My_String_To_Array = explode('', $My_String); throws an error:

Warning: explode() Empty delimiter

The PHP manual (http://php.net/manual/en/function.explode.php) explains:

If delimiter is an empty string (""), explode() will return FALSE.

So what should I be using in contemporary PHP instead of explode('', $My_String)?

So far, the alternatives I can see are:

  • split("", $My_String) - deprecated as of PHP 5.3.0
  • str_split($My_String)
  • preg_split('//', $My_String)

Seven years ago, str_split() would have been the correct alternative to use.

But is str_split() still in contemporary usage or should I only be looking at preg_split() ?

Or should I be looking at something else?

Cranwell answered 11/4, 2017 at 13:29 Comment(5)
Do you want to explode on a space? If so, make a space? explode(' ',$string). If you want to split a string into single characters, you can access that by doing $string[0] ... $string[6] and so on.Heterotypic
Possible duplicate of PHP: Split string into array, like explode with no delimiterSalas
There're no warnings or other alerts on str_split man page, so yes, it can be used.Locarno
"So what should I be using in contemporary PHP..." Can you tell us exactly what you want to achieve?Osuna
str_split() is perfectly valid. It is not deprecated and, most probably, it will never be. Don't mistake it for split() which is just an alias of explode(). preg_split() is not a replacement for str_split(). They have different arguments and work in different ways.Tiein
S
33

If dealing with multi-byte UTF-8 strings you should use:

$array = preg_split('//u', $My_String,-1, PREG_SPLIT_NO_EMPTY);

Otherwise you can just use:

$array = str_split($My_String);

The reason is noted in the manual:

str_split() will split into bytes, rather than characters when dealing with a multi-byte encoded string.

Starting from PHP version 7.4 the mbstring equivalent of str_split was added so you can now use:

$array = mb_str_split($my_string);

mb_str_split manual page

Sculpture answered 11/4, 2017 at 14:10 Comment(2)
A well-informed and comprehensive answer - thank you.Cranwell
Thanks. But that's dumb from PHP tbh. explode with empty delimiter should work.Haldas
F
12

I think str_split is what you are after

str_split — Convert a string to an array

<?php
$arr = str_split("Hello");
?>

will produce

Array
(
    [0] => H
    [1] => e
    [2] => l
    [3] => l
    [4] => o
}
Flunky answered 11/4, 2017 at 13:37 Comment(2)
Thank you, @MattJenkins. Can you confirm that str_split() still in contemporary usage? Is it still considered best practice in April 2017?Cranwell
No idea if it would be deemed best practice, however, I believe it does exactly what you require by design. Additionally, it's a valid and maintained method with no depreciation warnings.Flunky
S
2

the first parameter is required.

explode(separator,string,limit)

[separator]: required;
[string]: required;
[limit]: alternative.

An explode function with a void [separator] is meaningless.

That's all.O(∩_∩)O~

Shirtmaker answered 11/4, 2017 at 13:55 Comment(0)
C
2
//This will return error!!
 explode('', 'aaa');
//PHP Error:  explode(): Argument #1 ($separator) cannot be empty...

//Change to str_split
str_split('aaa');

[
     "a",
     "a",
     "a",
]
Complex answered 12/10, 2022 at 21:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.