I'm getting something like this: <h1> in inspect element, and I want to be able to remove all tags like this. I'm trying to have a clean meta tag description, but because I'm using description from rich text editor, those ampersand symbols are showing up.
I'm not sure what these are called?
They're not <h1>Title</h1> tags where I tried strip_tags(#notes), and didn't work.
These are called htmlentities. You had the right idea, to strip tags, but this function only works when they are decoded. It might be that you use html_safe at the form where they are typed in in the first place.
Install the HTMLentities gem (https://github.com/threedaymonk/htmlentities). Then decode your tags and then strip tags. You can encapsulate the methods, like strip_tags(coder.decode(#notes)).
But make sure you properly installed HTMLentities like it is described in the gem's readme.
You can use this:
function decodeHtml(html) {
var txt = document.createElement("textarea");
txt.innerHTML = html;
return txt.value;
}
// Example
alert(decodeHtml('Hello <strong>world</strong>!Hello <strong>world</strong>!'))
Related
Is there a tool which it can automatically cleanup unnecessary html codes, add missing html code pairs, remove wrong html code?
Something like this online tool
https://html-cleaner.com/
you can do it like this.
function stripe_woocommerce_short_description($the_excerpt) {
//wp_strip_all_tags will strip all HTML tags including script and style.
//return wp_strip_all_tags($the_excerpt);
//pass second parameter true if you want to remove break tag <br/> also.
//return wp_strip_all_tags( $the_excerpt, true );
//For Keep specific tags
return strip_tags($the_excerpt,"<img><table><p>");
}
add_filter('woocommerce_short_description', 'stripe_woocommerce_short_description',10, 1);
I have a sort of strange use-case in Angular 2 where I have some content that contains regular html tags as well as custom html tags. I want to render the regular html tags and show the custom html tags as plain text. For example
the <CUSTOM_TAG>boy</CUSTOM_TAG> went to the <b>store</b>
should have <CUSTOM_TAG>boy</CUSTOM_TAG> appearing as plain text just as you see it above, however <b>store</b> should appear as store i.e. the bold tag is actually rendered.
When I try the usual way of inserting html i.e.
<div [innerHtml]="myHtml"></div>
I get a sanitization error because of the custom tag. When I fix the sanitization error as was done here it just strips out the custom tags which I also don't want. Is showing the custom tags as plain text and rendering the regular html tags possible?
If all the possible custom tags are known, you can encode them before passing the string to the [innerHTML] binding. The method encodeCustomTags in the following code snippet uses a regular expression to replace <customTag> with <customTag>:
private customTags = [
"CUSTOM_TAG",
"otherTag",
];
myHtml = this.encodeCustomTags("the <CUSTOM_TAG>boy</CUSTOM_TAG> went to the <b>store</b>");
private encodeCustomTags(html: string): string {
let regex: RegExp;
for (let tag of this.customTags) {
regex = new RegExp(`<(/?)${tag}>`, "gi");
html = html.replace(regex, `<$1${tag}>`)
}
return html;
}
See this stackblitz for a demo.
I have a JSON object that looks like the following:
id:
text: <h1>This is my text</h1> <p> I want to include HTML
and reflect those tags on the page. </p>
I'm using Angular2's HTTP_PROVIDER to read the data from the JSON.
In my HTML template, I am displaying the JSON.dataString on the webpage. How do I reflect the HTML tags on the webpage, currently the tags are displayed as plain text.
<p>{{jsonObject.text}}</p>
Is there a way to read in those HTML tags that are included in the JSON objects, and have them reflected on the webpage?
Something like:
<div [innerHTML]="jsonObject.text"></div>
Should display the text object as raw HTML. Be careful about XSS injection when you do something like this.
More detail at this question.
You may try to do it like this:
function textHtml(input) {
var el = document.createElement("textarea");
el.innerHTML = input;
return el.value;
}
And then use this function to get text with tags
I don't use Angular but do something like that.
<p id="myId"></p>
<script>
document.getElementById("myId").appendChild(jsonObject.text);
</script>
I did not test it.
I'm trying to implement a markup system in my Rails application using the bb-ruby gem. Currently I'm working on something similar to how Stackoverflow handles it's code markdown and I ran into some difficulty.
Essentially I want the user-entered text:
[code]<h1>Headline</h1>[/code]
To spit out the code in plain-text, perhaps in a pre and code tag block. Passing that string of text to my code parser will wrap the code in a pre and code block but the HTML also gets rendered. I pass the string to my code parser like so:
sanitize(text.bbcode_to_html(formats, false).html_safe)
Of course, if I remove the .html_safe helper from the call my view will spit out:
<pre><code><br /> <h1>Hello World</h1><br /> </code></pre>
Obviously that's not the desired result. So my question is, how can I accomplish plain-text code only within the pre + code tags while maintaining the html_safe helper method?
I know this is an old question but you can try using the strip_tags after the bbcode_to_html one.
I want to parse a web page in Groovy and extract all of the href links and the associated text with it.
If the page contained these links:
Google<br />
Apple
the output would be:
Google, http://www.google.com<br />
Apple, http://www.apple.com
I'm looking for a Groovy answer. AKA. The easy way!
Assuming well-formed XHTML, slurp the xml, collect up all the tags, find the 'a' tags, and print out the href and text.
input = """<html><body>
John
Google
StackOverflow
</body></html>"""
doc = new XmlSlurper().parseText(input)
doc.depthFirst().collect { it }.findAll { it.name() == "a" }.each {
println "${it.text()}, ${it.#href.text()}"
}
A quick google search turned up a nice looking possibility, TagSoup.
I don't know java but I think that xpath is far better than classic regular expressions in order to get one (or more) html elements.
It is also easier to write and to read.
<html>
<body>
1
2
3
</body>
</html>
With the html above, this expression "/html/body/a" will list all href elements.
Here's a good step by step tutorial http://www.zvon.org/xxl/XPathTutorial/General/examples.html
Use XMLSlurper to parse the HTML as an XML document and then use the find method with an appropriate closure to select the a tags and then use the list method on GPathResult to get a list of the tags. You should then be able to extract the text as children of the GPathResult.
Try a regular expression. Something like this should work:
(html =~ /<a.*href='(.*?)'.*>(.*?)<\/a>/).each { url, text ->
// do something with url and text
}
Take a look at Groovy - Tutorial 4 - Regular expressions basics and Anchor Tag Regular Expression Breaking.
Parsing using XMlSlurper only works if HTMl is well-formed.
If your HTMl page has non-well-formed tags, then use regex for parsing the page.
Ex: <a href="www.google.com">
here, 'a' is not closed and thus not well formed.
new URL(url).eachLine{
(it =~ /.*<A HREF="(.*?)">/).each{
// process hrefs
}
}
Html parser + Regular expressions
Any language would do it, though I'd say Perl is the fastest solution.