Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to strip HTML tags including escaped image tags without ' and   stuff?

I need to strip all the html elements from my content. Example of the content :

"<p><img alt=\"\" src=\"/ckeditor_assets/pictures/1/content_twitter-sink.jpg\" style=\"width: 570px; height: 399px;\" /></p>\r\n\r\n<h3 style=\"font-size: 1.38462em; margin: 1em 0px 0px; font-weight: 600; line-height: 1.2; font-family: freight-sans-pro, sans-serif; -webkit-font-smoothing: antialiased; text-rendering: optimizelegibility; color: rgb(46, 46, 46);\">Soraya Calavassy, communications manager at the Award in Australia,&nbsp;shares her organisation&#39;s experience&nbsp;piloting our new global visual identity.</h3>\r\n\r\n<p style=\"margin: 0.5em 0px 0px; text-rendering: optimizelegibility; font-size: 1.30769em; line-height: 1.3; color: rgb(78, 78, 78); font-family: freight-sans-pro, sans-serif;\">&quot;While Australia has a very strong brand locally, there are some great benefits for incorporating"

When displaying the post i use raw which does give me the output I need. But I need to generate an excerpt. For that, I need to strip all the HTML tags and even remove the images. But when I use sanitize it won't remove the images. If I use strip_tags , it will remove the images but it will add &#39; for apostrophe, &nbsp; for space, etc. So, how to get a clean excerpt without images and without &nbsp; stuff?

like image 759
THpubs Avatar asked Feb 04 '13 04:02

THpubs


People also ask

Which function will remove the HTML tags from data?

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.

Which function is used to remove all HTML tags from a string passed to a form?

The strip_tags() function strips a string from HTML, XML, and PHP tags. Note: HTML comments are always stripped.


2 Answers

try strip_tags(text).html_safe

like image 104
jvnill Avatar answered Sep 23 '22 23:09

jvnill


Doesn't work with rails 4.1

Only way by combing strip_tags with the gsub function (link below)

Ruby gsub multiple characters in string

So in helper, I'd do:

def format_text(string)
    strip_tags(string).gsub("&nbsp;", "").gsub("&#39;", "'")
end
like image 45
Bilton Tran Avatar answered Sep 22 '22 23:09

Bilton Tran