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?

4
  • 1
    possible duplicate of Remove empty array elements Commented Jul 4, 2014 at 3: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] => ) Commented Jul 4, 2014 at 3: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. Commented Jul 4, 2014 at 4:08
  • And FYI OP -- typically, you're supposed to say what you've tried in your OP. Commented Jul 4, 2014 at 4:09

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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));

Comments

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/>'));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.