• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

how can i trim at the last occurance of a character?

New Here ,
Mar 18, 2008 Mar 18, 2008

Copy link to clipboard

Copied

Let's say I've got a set of strings being drawn from a database that look like this:
"<p><strong>This is a test<stro" (NEEDS TRIMMING)
"<p>This is a test 2</p><br />Hi." (GOOD)
"<i>This is a test 3</i" (NEEDS TRIMMING)

I want to trim my results so that the two strings with incomplete HTML become:
"<p><strong>This is a test" (TRIMMED)
"<i>This is a test 3" (TRIMMED)

So basically I want to trim off from "<" to the end of my string, but only in the situation that a finalizing ">" is not present.

Normally I would think to make my strings into a list delimited by "<" and then removing the last item, but that would also trim all the GOOD statements that have terminating ">"'s.

Can someone lead me in the right direction? I've been scratching my head over this for a couple of days.

Thanks,
Sam
TOPICS
Advanced techniques

Views

406

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Mar 18, 2008 Mar 18, 2008

Copy link to clipboard

Copied

Hi,

Try this SafeText UDF!...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 18, 2008 Mar 18, 2008

Copy link to clipboard

Copied

I guess I'll have to do some kind of cfscript, my problem is not the removal of bad tags though, but having incomplete tags in my strings

The user is limited to the number of characters that can be displayed, I dont want it to chop off in the middle of an HTML tag

I need some kind of loop that scans the string and checks for a finalizing >, and then removes all text up to the previous <

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 19, 2008 Mar 19, 2008

Copy link to clipboard

Copied

Hi,
why dont u make use of regular expressions..

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 19, 2008 Mar 19, 2008

Copy link to clipboard

Copied

LATEST
<cfscript>
strings = arrayNew(1);
strings[1] = structNew();
strings[1].old = "<p><strong>This is a test<stro";
strings[2] = structNew();
strings[2].old = "<p>This is a test 2</p><br />Hi.";
strings[3] = structNew();
strings[3].old = "<i>This is a test 3</i";
strings[4] = structNew();
strings[4].old = "<i>This is a test 4<";

for (i=1; i LTE arrayLen(strings); i=i+1) {
strings .new = rereplace(strings.old, '\<([^\>]+)?$', '');
}


</cfscript>

<cfdump var="#strings#" />

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation