user1481563
user1481563

Reputation: 125

Remove HTML tags from strings in a flat array then join with commas

I have an array that contains a set of links as strings e.g.

[1] => "<a href = 'this.html'> This </a>"
[2] => "<a href = 'that.html'> That </a>"
[3] => "<a href = 'other.html'> Other </a>"

What is the easiest way to echo out just the text separated by comas? e.g. so it displays:

This, That, Other

Upvotes: 1

Views: 309

Answers (2)

Filip Ros&#233;en
Filip Ros&#233;en

Reputation: 63872

You'll require the use of two functions, implode and strip_tags.

$data = array (
  "<a href='this.html'>This</a>",
  "<a href='this.html'>That</a>",
  "<a href='this.html'>Other</a>"
);

echo strip_tags (implode (", ", $data));

This, That, Other

Links to documentation

  • implode

    This function will glue together the elements of the array passed as second parameter with the "glue" specified as the first. implode (":", array (1,2,3)) will result in "1:2:3"

  • strip_tags

    This function will remove the xml-elements (tags) found in the string passed as first parameter.

Upvotes: 4

Lake
Lake

Reputation: 913

implode(",",array_map('strip_tags',$yourarray));

Try this

Upvotes: 3

Related Questions