How to remove more than one whitespace character from HTML? - html

I want to remove extra whitespace which is coming from the user end, but I can't predict the format of the HTML.
For example:
<p> It's interesting that you would try cfsetting, since nothing in it's
documentation would indicate that it would do what you are asking.
Unless of course you were mis-reading what "enableCFoutputOnly" is
supposed to do.
</p>
<p>
It's interesting that you would try cfsetting, since nothing in it's
documentation would indicate that it would do what you are asking.
Unless of course you were mis-reading what "enableCFoutputOnly" is
supposed to do.</p>
Please guide me on how to remove more than one whitespace character from HTML.

You could use regex to replace any cases of multiple whitespace characters with a single space by looping over the result until no more multiple whitespace occurances exist:
lastTry = "<p> lots of space </p>";
nextTry = rereplace(lastTry,"\s\s", " ", "all");
while(nextTry != lastTry) {
lastTry = nextTry;
nextTry = REReplace(lastTry,"\s\s", " ", "all");
}
Tested working in CF10.

if you don't want to do it thru code out of total lazyness
=> http://jsbeautifier.org/
if you want to do it by code then a regex would be another option

This should do it:
<cfscript>
string function stripCRLFAndMultipleSpaces(required string theString) {
local.result = trim(rereplace(trim(arguments.theString), "([#Chr(09)#-#Chr(30)#])", " ", "all"));
local.result = trim(rereplace(local.result, "\s{2,}", " ", "all"));
return local.result;
}
</cfscript>

Related

Putting HtmlWidget elements in a paragraph

May I know how to put the elements I get from HtmlWidget(allwordlist![0].ivakavakayagataki.toString()), HtmlWidget(allwordlist![0].ivakavakayagataki.toString()), in a paragraph?
I am making a dictionary application and the above HTML render code works but problem is that I cannot seem to find a way to put these two in a paragraph as in side by side like a sentence.
May I get some help un knowing how to do this please.
Thanks in advance.
Ok so I made it work.
So I first made this function and not that <p> tags so the problem thats was making my HtmlWidget statement not inline and having line breaks was because of the p tag so I first took it out.
Widget mergehtml(String html1, String html2,){
html1 = html1.replaceAll("<p>", "").replaceAll("</p>", "");
html2 = html2.replaceAll("<p>", "").replaceAll("</p>", "");
String mergedHtml = "<p style='font-size:18px;'>" + html1 + " " + "[" + html2 + "], " + "</p>" ;
return HtmlWidget(mergedHtml);
}
and then I called it and put my two variables there.
mergehtml("${allwordlist![0].tina}","${allwordlist![0].itavi}",)
it then started working inline.
Thanks :)

Find the word and replace with html tag using regex

I have a text equation like: 10x^2-8y^2-7k^4=0.
How can I find the ^ and replace it with <sup>2</sup> in the whole string using regex. The result should be like:
I tried str = str.replace(/\^\s/g, "<sup>$1</sup> ") but I’m not getting the expected result.
Any ideas that can help to solve my problem?
I think you're looking for something like
\^(\d+)
It matches the ^, captures the exponent and replace with
<sup>$1</sup>
See it here at regex101.
Edit:
To meet your new demands, check this fiddle. It handles the sub as well using replace with a function.
Your current pattern matches a caret followed by a space character (space, tab, new-line, etc.), but you want to match a caret followed by a single character or multiple characters wrapped in accolades, as your string is in TeX.
/\^(?:([\w\d])|\{([\w\d]{2,})\})/g
Now, using str = str.replace(/\^(?:([\w\d])|\{([\w\d]{2,})\})/g, "<sup>$1</sup>"); should do the job.
You can make a more generic function from this expression that can wrap characters prefixed by a specific character with a specific tag.
function wrapPrefixed(string, prefix, tagName) {
return string.replace(new RegExp("\\" + prefix + "(?:([\\w\\d])|\\{([\\w\\d]{2,})\\})"), "<" + tagname + ">$1</" + tagname + ">");
}
For instance, calling wrapPrefixed("1_2 + 4_{3+2}", "_", "sub"); results in 1<sub>2</sub> + 4<sub>3+2</sub>.

Removing Quote inside <title> tag

The code I used to Produce This is the same as the one WITHOUT this problem
(just pulling from a different database column)
if (!string.IsNullOrEmpty(childspec.titlew)) {
wtitle = childspec.titlew;
ltlMasterPageTitle.Text = HttpUtility.HtmlDecode(wtitle) + "";
} else {
wtitle = childspec.laytitle;
ltlMasterPageTitle.Text = "Company Name - " + HttpUtility.HtmlDecode(wtitle);
}
here is the code that does the output.
When its the ELSE case then the content does not have a wrapper around this
not sure if this is a serious SEO issue as well**
ALSO how do i remove this extra Tab space infront of the text?
I still don't know where the extra space came from but I just applied
Trim()
again before the output code and it removed all extra spacing and quotes

Convert QString to text with substitutes for HTML special characters (e.g. tags)

The user will be able to put in some text into a QLineEdit in a Qt environment. However, these input texts can contain HTML special characters. My aim is to convert this text by replacing all HTML special character occurences with substitutes.
A similar case is found in PHP with the htmlspecialchars() function http://php.net/manual/en/function.htmlspecialchars.php.
The main reason I want to do this is because I want to display the user input in a richtext QTextEdit and I don't want the user to be able to change HTML and I wish to be able to use HTML special characters without too much hassle.
How can this be achieved?
The easiest way I know, is to use QTextEdit::toHtml:
QString convert();
{
QString s = lineEdit->text();
QTextEdit textEdit;
textEdit.setPlainText(s);
QString ret = textEdit.toHtml();
int firstClosingTag = ret.indexOf("</p></body></html>");
int lastOpeningTag = ret.lastIndexOf(">", firstClosingTag);
return ret.mid(lastOpeningTag + 1, firstClosingTag - lastOpeningTag - 1);
}
There are also two functions, which you could find useful:
Qt::convertFromPlainText() and Qt::escape()
In Qt5, it's QString::toHtmlEscaped, e.g.:
QString a = "Hello, <span class=\"name\">Bear</span>!";
// a will contain: Hello, <span class="name">Bear</span>!
QString b = a.toHtmlEscaped();
// b will contain: Hello, <span class="name">Bear</span>!
This is direct equivalent of the htmlspecialchars in PHP. It replaces the Qt::escape function (mentioned by Amartel), which does the same thing but is now obsolete.
The Qt::convertFromPlainText function (also mentioned by Amartel) still exists in Qt 5, but it does more than PHP's htmlspecialchars. Not only it replaces < with <, > with >, & with &, " with " but also does additional handling of whitespace characters (space, tab, line feed, etc) to make the generated HTML look visually similarly to the original plain text. Particularly, it may put <p>…</p>/<br> for linefeeds, non-breaking spaces for spaces and multiple non-breaking spaces for tabs. I.e. this function is not just htmlspecialchars, it's even more comprehensive than nl2br(htmlspecialchars($s)) combination.
Note that unlike the PHP's htmlspecialchars with ENT_QUOTES, none of the Qt functions listed in this answer replace single quote (') with &apos;/'. So, for example, QString html = "<img alt='" + s.toHtmlEscaped() + "'>"; won't be safe, only QString html = "<img alt=\"" + s.toHtmlEscaped() + "\">"; will. (However, as < is replaced and ' has no special meaning outside <…>, something like QString html = "<b>" + s.toHtmlEscaped() + "</b>"; would also be safe.)

Replace continuous space with single space and multiple "&nbsp" elements

I have one html document which contains whitespaces in some nodes. For example,
<B>This is Whitespace Node </B>
When this html is displayed in the browser, more than one continuous space in html is always displayed as one space. To avoid this issue, I want to replace the continuous spaces with a single space and multiple elements.
What is the best solution to achive this?
I am using C# 2005.
Try this,
string str = "<B>This is Whitespace Node </B>";
Regex rgx = new Regex("([\\S][ ])");
string result = rgx.Replace(str, "$1.")
.Replace(" .","?")
.Replace(" ","&nbsp")
.Replace("?"," ");
Use CSS's white-space property as per http://www.w3.org/TR/CSS2/text.html#white-space-prop
white-space: pre-wrap
Or, if you really want to do it with bruteforce, replace two consecutive spaces with a non-breaking-space and a normal space... I strongly recommend against this.
string text = originalText.Replace(" ", " ");
You can try
String.Replace(" ", " ")
if you prefer regex
Regex rgx = new Regex("([ \t]|&nsbp)+");
string result = rgx.Replace(input, " ");
I assume you are setting the value of the control from code behind? If so then ...
<strong><asp:Literal id="myLiteral">This is Whitespace Node </asp:Literal></strong>
And in code behind ...
var myText = "This is Whitespace Node ";
myLiteral.Text = myText.Replace(" ", " ");
If no code behind or not in a literal ...
<strong><%= "This is Whitespace Node ".Replace(" ", " ") %></strong>