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, shares her organisation's experience 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;\">"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 '
for apostrophe,
for space, etc. So, how to get a clean excerpt without images and without
stuff?
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.
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.
The strip_tags() function strips a string from HTML, XML, and PHP tags. Note: HTML comments are always stripped.
try strip_tags(text).html_safe
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(" ", "").gsub("'", "'")
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With