ColdFusion EncodeForHTML and newlines - replace

What characters do newlines (\n) and carriage returns (\r) get converted to when using EncodeForHTML in ColdFusion? I have tried everything I can think of (or find online) but cannot find what I need to use in my REReplace statement to convert to break (br) tags after the encoding (I need to do this for display purposes).
What I would like to do is something like
#REReplace(EncodeForHTML(myVar), "html encoded newline etc chars", "<br />", "all")#
However, because I can't figure out what the newlines and carriage returns are being converted to the only way I can get it to work it to do a REReplace before and after EncodeForHTML, which doesn't seem very sensible or efficient. For example:
#REReplace(EncodeForHTML(REReplace(myVar, "\r\n|\n\r|\n|\r", "<br />", "all")), "<br &##x2f;>", "<br />", "all")#
I am using CF 10.

\n is being encoded as
\r is being encoded as
So the following simplified code now works:
#REReplace(EncodeForHTML(myVar), "&##xa;&##xd;|&##xd;&##xa;|&##xa;|&##xd;", "<br />", "all")#

Related

How to replace only quotes inside a specific tag

How to replace only some quotes and retain all the content, Please see the below example
<type>"sample"<type>
<content>R.A.H.M.A.N. sample text."</content>
to
<type>"sample"<type>
<content>R.A.H.M.A.N. sample text.</content>
Replace the quote inside content tag only, is it possible?
If all the <content> tags are on a line by themselves, you can use the :g command:
:g=<content>.*</content>=s="==g
This will replace all quotes on all lines that contain an open and close <content> tag. (This does not necessarily mean those quotes are inside the <content> tag.)
See :help global.
:%s!<content>\zs\_.\{-}\ze</content>!\=substitute(submatch(0), '"', '', 'g')!g
The idea is:
Run a substitution command on all lines and for all hits in a single line: :%s!...!...!g
Search for <content>, followed by 0 or more characters (as few as possible), followed by </content>: `_.{-}
... but set the start of the match just after <content> and the end of the match just before </content>, respectively: <content>\zs...\ze</content>
This means we're not substituting the <content> and </content> parts themselves, just the contents.
Replace it by the result of running an expression: \=
The expression is substitute(submatch(0), '"', '', 'g'):
Take the text matched so far: submatch(0)
Search for all " and replace them by nothing: substitute(..., '"', '', 'g')

How to replace <br /> with a carriage return so it displays in a textarea properly?

I am using this code to get in a textarea value on my form:
EncodeForHTML(REReplaceNoCase(Trim(Form.Description), '\r', '<br />', 'all'))
It is simply replacing all carriage returns (new line) in the textarea with an HTML tag of <br />. It then HTML encodes this before inserting into the database.
When I want to display the textarea value back into a text area I am doing this:
<textarea>#Canonicalize(Description, true, true)#</textarea>
All its doing is Canonicalizing/Decoding the HTML value into a proper readable format.
The problem I have is that the textarea shows the <br /> as part of the string in the text box area. I don't want it to show this because its not part of the description string. Its just indicating a new line.
How could I fix this so that the <br /> in the string is translated into a carraige return for the textarea to display the contents properly?
You should replace your <br /> with Chr(13) & Chr(10).
As described in the documentation:
Chr(10) returns a linefeed character
Chr(13) returns a carriage return character
The two-character string Chr(13) & Chr(10) returns a Windows newline

Stop Dreamweaver converting find replace queries when loaded via dreamweaver.setUpComplexFindReplace

I want a find/replace that matches the HTML encoded tab character and replace it with a space.
When I load the following query via Dreamweaver's dreamweaver.setUpComplexFindReplace API function, the is converted to a literal tab character, so no matches are made.
How do I stop the HTML encoded string from being converted?
dreamweaver.setUpComplexFindReplace('<dwquery> <queryparams matchcase="false" ignorewhitespace="false" useregexp="false" wholeword="true" /> <find searchmode="document"> <qtext qname=" " qraw="true"></qtext> </find> <replace action="replaceText" param1="" param2=""/></dwquery>');
dreamweaver.replaceAll();
In your qtext node, change the qname value from to &#9.

How can I reduce three or more repetitions of some text to only two?

I've got a text. I want to find out if a certain part of that text is repeated three or more times and replace that by only two repetitions.
For example, in the HTML code I'm looking at, there are 3 or more <br /> in a row and I want to change that to just 2 <br /> in a row.
How can I do that?
Is this what you want?
<?php
$s='<br /><br /> <br />';
$s=preg_replace('#(<br />\s*<br />)(?:\s*<br />)+#', "$1", $s);
print($s);
?>
If there are more than 2 consecutive <br /> tags (not counting whitespace), delete all but the first two.
Edit: As noted by Tim below, my original answer was altogether incorrect.
The correct regex for replacement would look like:
$s = preg_replace('/(.)\1{2,}/', '$1$1', $s);
It means: match any character once, then the same character (\1) at least twice more ({2,}), and replace the entire matched set with the first character, but only 2 times.
However, it might be that the above answers are probably closer to what you want.
For posterity, my original, incorrect regex looked like: /(.){3,}/ig
Not sure if it's possible to do this with a single regex. You probably need something like this:
$temp = preg_split('/<br \/>/', $input, 3);
if (count($temp) == 3) {
$temp[2] = str_replace('<br />', '', $temp[2]);
}
$result = implode($temp, '<br />');
By the way: it's not a good idea to use regular expressions for HTML parsing
If it is just <br /> you are trying to replace and not multiple patterns then this should work:
$s = preg_replace('/(<br />){3,}/', '<br /><br />');
If you need to match several different strings then this won't work.

Coldfusion RegEx replace html characters like

I currently have code to remove html from a string:
<cfset arguments.textToFormat = REReplaceNoCase(arguments.textToFormat,"<[^>]*>","","ALL") />
However, this does not remove html characters like
What Regex could I use to ReReplace these characters??
Thanks
For removing and other similar strings :
&[^;]+?;
HTH