Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove html elements leaving text content in PHP

Tags:

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>
like image 505
user1931956 Avatar asked Jul 13 '13 11:07

user1931956


People also ask

How do you remove HTML tags from data in PHP?

PHP provides an inbuilt function to remove the HTML tags from the data. The strip_tags() function is an inbuilt function in PHP that removes the strings form HTML, XML and PHP tags. It accepts two parameters. This function returns a string with all NULL bytes, HTML, and PHP tags stripped from a given $str.

How do I strip a string in HTML?

To strip out all the HTML tags from a string there are lots of procedures in JavaScript. In order to strip out tags we can use replace() function and can also use . textContent property, . innerText property from HTML DOM.


1 Answers

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

like image 137
Steven Moseley Avatar answered Oct 19 '22 23:10

Steven Moseley