简体   繁体   中英

Explode() function not working as expected

This might be an easy thing to fix but i still have to ask because it should not be happening in the first place.

I have this simple string: 24.00C 48%

Now when using the explode() function on this string it should return an Array with 2 Elements.

$str = "24.00C 48%";
$str_array = explode(" ", $str);

Expected Result

$str_array[0] = 24.00C
$str_array[1] = 48%

What actually happens

The first element of the array is correct and it contains what it should ( 24.00C ) now the second element of the array was empty so i checked what the array actually contains using print_r

Now what it returned is what confuses me the most.

Array ( [0] => 24.00C [1] => [2] => [3] => [4] => [5] => [6] => [7] => 48% )

As you can see the first element is fine but after that its not really doing what it should..

Now my questions are:

Does anyone know why this is happening and how to make explode work properly?

Also why is the 48% the 7th Element and not the 3rd as example is there any specific reason for that?

I think you have mistaken on how to print, it should work properly.

Code:

$str = "24.00C 48%";
$str_array = explode(" ", $str);

echo $str_array[0] . "<br>";
echo $str_array[1] . "<br>";

print_r($str_array);

Result:

24.00C
48%
Array ( [0] => 24.00C [1] => 48% ) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM