user1931956
user1931956

Reputation: 61

Remove html elements leaving text content in PHP

Hi please any one give me a soloution for this. I want to remove the entire div and span from this html except 4 in <span>. Any one please

<div class=\"fivestar-widget-static fivestar-widget-static-vote fivestar-widget-static-5 clear-block\">
  <div class=\"star star-1 star-odd star-first\">
    <span class=\"on\">4</span>
  </div>
  <div class=\"star star-2 star-even\">
    <span class=\"on\"></span>
  </div>
  <div class=\"star star-3 star-odd\">
    <span class=\"on\"></span>
  </div>
  <div class=\"star star-4 star-even\">
    <span class=\"on\"></span>
  </div>
  <div class=\"star star-5 star-odd star-last\">
    <span class=\"off\"></span>
  </div>
</div>

Upvotes: 6

Views: 6833

Answers (1)

Steven Moseley
Steven Moseley

Reputation: 16325

To strip HTML tags but leave their text content, you can just use strip_tags:

$string = "<div>Hello</div> <span>Hi</span> Other text";
$string = strip_tags($string);

// You'll probably also want to trim the results 
// to remove extraneous whitespace
$string = trim($string);

Which will result in "Hello Hi Other text"

PHP Manual: http://php.net/manual/en/function.strip-tags.php

Upvotes: 11

Related Questions