Skip to main content
Participant
March 18, 2008
Question

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

  • March 18, 2008
  • 4 replies
  • 461 views
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
This topic has been closed for replies.

4 replies

Inspiring
March 19, 2008
<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#" />
Known Participant
March 19, 2008
Hi,
why dont u make use of regular expressions..
samsjlAuthor
Participant
March 18, 2008
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 <
Inspiring
March 18, 2008
Hi,

Try this SafeText UDF!...