0

Hi i have a string which has <br/> in it, which is converted to an array.

$files = "Pretty Hurts<br/>
          Ghost/Haunted<br/>
          Drunk in Love (feat. Jay-Z)<br/>
          Blow<br/>
          No Angel<br/>
          Yoncé/Partition<br/>
          Jealous<br/>
          Rocket<br/>
          Mine (feat. Drake)
          <br/>"
 $files_to_array = explode('<br/>', $files);

when i print_r the array there's an empty element in the array i tried trimming the string before i add it to the array, empty elemnt still appears. how can i solve this?

Junius L
  • 15,881
  • 6
  • 52
  • 96
  • 1
    possible duplicate of [Remove empty array elements](http://stackoverflow.com/questions/3654295/remove-empty-array-elements) – jeremy Jul 04 '14 at 03:47
  • @Jeremy i i tried those solutions but they are not working, when is `print_r(array_filter($files_to_array));` this is what i get `Array ( [0] => Pretty Hurts [1] => Ghost/Haunted [2] => Drunk in Love (feat. Jay-Z) [3] => Blow [4] => No Angel [5] => Yoncé/Partition [6] => Jealous [7] => Rocket [8] => Mine (feat. Drake) [9] => )` – Junius L Jul 04 '14 at 03:54
  • While I wasn't the downvoter, I'd say that you're probably doing it wrong if other solutions didn't work for you. – jeremy Jul 04 '14 at 04:08
  • And FYI OP -- typically, you're supposed to say what you've tried in your OP. – jeremy Jul 04 '14 at 04:09

3 Answers3

2

You can use array_filter($files_to_array, "trim"). array_filter without the second parameter returns all values from the original array that aren't considered equal to false. An empty string is equal to false, but a string of whitespace is not. trim overcomes this.
PHP array_filter manual page

faintsignal
  • 1,828
  • 3
  • 22
  • 30
0

we have array_filter function for that

simply do this

array_filter($files_to_array);

and to rebuilt the sequential keys use array_values

so it'll be array_values( array_filter($files_to_array));

ɹɐqʞɐ zoɹǝɟ
  • 4,342
  • 3
  • 22
  • 35
0
$files = "Pretty Hurts<br/>
      Ghost/Haunted<br/>
      Drunk in Love (feat. Jay-Z)<br/>
      Blow<br/>
      No Angel<br/>
      Yoncé/Partition<br/>
      Jealous<br/>
      Rocket<br/>
      Mine (feat. Drake)
      <br/>";
$files_to_array = explode('<br/>', trim($files, '<br/>'));
mylxsw
  • 86
  • 2
  • 4