Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get only text from html tag

I have query some data and result is like this

<p><img src="xxx.png" alt="" style="margin&#58;5px;" /><br></p><p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.<br></p>

show on console. I want to remove all html tag from this data and get only string like this

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type.

Anyone know how to remove single and double quote from this data or some solution. Thanks

like image 723
Kem Bardly Avatar asked Oct 26 '25 22:10

Kem Bardly


1 Answers

You can create a temporary element and read it's .textContent property:

var d = document.createElement('div');
d.innerHTML = htmlContent;
var textContent = d.textContent || d.innerText;

If you can use jQuery:

var textContent = $('<div/>').html(htmlContent).text();
like image 186
Siah Avatar answered Oct 29 '25 13:10

Siah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!