1

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?

Freddy789
  • 506
  • 4
  • 15
  • 4
    you have hidden whitespace characters. Try doing `explode(' ', trim($str))` – treyBake Aug 16 '19 at 13:44
  • @treyBake nothing changed same result – Freddy789 Aug 16 '19 at 13:45
  • 2
    @treyBake `trim` won't help as the hidden whitespace is inside the string, not on the ends. – Nick Aug 16 '19 at 13:46
  • 2
    What do you get when you run the split for `$str_array = preg_split("/\s+/", $str);` where `\s+` matches 1+ whitespace characters. – The fourth bird Aug 16 '19 at 13:46
  • @Nick huh, didn't know that ^.^ though, in this case, is that still irrelevant? o.O (as in if it's either side of the string, surely we'd see elements either side of the actual values in the explode? – treyBake Aug 16 '19 at 13:47
  • 1
    Quick fix: `array_filter(explode(" ", $str))` – Justinas Aug 16 '19 at 13:48
  • @Thefourthbird that actually just fixed it ^-^ thank you... but just woundering now is it possible to remove the whitespaces to make `explode` work? – Freddy789 Aug 16 '19 at 13:50
  • @Justinas i mean you answer does what it should but the "second" element is the the 7th element of the array... – Freddy789 Aug 16 '19 at 13:55
  • `explode(" ", str_replace("C", "C ", str_replace(" ","", "24.00C 48%"))); ` would work to remove all the white space and put back a single space after C, then explode, but it's ugly code – Brett Gregson Aug 16 '19 at 13:57
  • @Freddy789 FWIW, I just ran your two lines and I got the correct result. I'm going to follow this question to see what the story is. – dazed-and-confused Aug 16 '19 at 13:57
  • 1
    @Freddy789 Try to replace if exist any white spaces like this `$str = preg_replace('!\s+!', ' ', $str);` after that `$array=explode(" ", $str);` Source: https://stackoverflow.com/questions/2368539/php-replacing-multiple-spaces-with-a-single-space – Serghei Leonenco Aug 16 '19 at 13:58
  • @dazed-and-confused the two lines is the end of a bunch of ugly `str_replace()` and `preg_replace()` i assume in that process some whitespaces made their way into the string – Freddy789 Aug 16 '19 at 14:00
  • 1
    I am not sure what exactly is that space is between your string, but perhaps copying that and paste it as the delimiter in explode and see if it works. I would not recommend that though, as you are not really sure what is so you can not rely on it. Using `explode(" ", preg_replace("/\s+/", " ", $str))` would be overkill as you could use the preg_split variant. Note that `\s` would also match a newline. So perhaps using `\h` to match horizontal whitespace chars would be better `$str_array = preg_split("/\h+/", $str);` – The fourth bird Aug 16 '19 at 14:00
  • 1
    @Thefourthbird using \h does the job perfectly fine aswell... and yes i agree using `explode(" ", preg_replace("/\s+/", " ", $str))` would be too much but its still intresting to play around with all the possible solutions^^ also i just want to say thanks everyone for your quick responses really helped me learn something new and useful today :) – Freddy789 Aug 16 '19 at 14:10
  • @Thefourthbird if you want to write an answer with your solution so i can accept it also for other people with a simular problem – Freddy789 Aug 19 '19 at 07:40

1 Answers1

0

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% ) 
Karlo
  • 242
  • 2
  • 7
  • As i said in the comments the string is build by using loads of `replace` function and during that process some whitespaces made their way into the string. The solution from @the fourth bird `$str_array = preg_split("/\s+/", $str)` solves the problem perfectly – Freddy789 Aug 19 '19 at 06:21