Solved

truncate string

Posted on 2006-06-30
7
887 Views
Last Modified: 2012-05-05
HI,

I'm trying to shorten a string from the database to show it as a preview.
I use the left function to achieve this but I have HTML codes for special characters. Like ' for single quotes, ë for ë, etc. The string can also contain a link so the length of the preview isn't always the same and in some cases it returns the start of a bit of code.

  This is just some sample text to show the 'problem&#...
or
  This is just some <A HREF="http://www.valerysuper.be" TARGE...

Is there a simple way to shorten the "output" of the string?
0
Comment
Question by:seloh
  • 3
  • 2
  • 2
7 Comments
 
LVL 4

Expert Comment

by:FSIFM
ID: 17016448
You could convert it all to HTML before doing the left function

Server.HTMLEncode(rst("Field")) - this should conver the ASCII values to HTML then perform the left function.

FSIFM
0
 
LVL 2

Author Comment

by:seloh
ID: 17016740
I have in the database the following string:

On Saturday 23rd of september 2006 is the final for the &#39;the golden twelve&#39; in domain <A HREF="http://www.vossenberg.be" TARGET="_new" CLASS="body">Vossenberg</A> in Hoog. Tickets can be purchased for &#8364; 85,00 per person (all-in).<BR>&#39;The golden twelve&#39; are the girls that will advance to the final of the miss belgian beauty contest which will be held in the Casino of Knokke.<BR>This final will be on saturday the 21st of october, tickets for the show and gala-dinner are stil available for &#8364; 195,00 per person (all-in).<BR><BR>Details information and the menu can be found on <A HREF="http://www.missbelgianbeauty.be" TARGET="_new" CLASS="body">www.missbelgianbeauty.be</A>, and choose &#39;the golden twelve&#39; or &#39;the final&#39;.<BR><BR>Be quick to order your tickets now that they are still available!

If I use left(RS("news"), 100) &"..." it will return the preview:
On Saturday 23rd of september 2006 is the final for the 'the golden twelve' in domain <A HR...

If I use left(RS("news"), 250) &"..." it will return the preview:
On Saturday 23rd of september 2006 is the final for the 'the golden twelve' in domain Vossenberg in Hoog. Tickets can be purchased for € 85,00 per person (all-in).
&#...

If I use left(RS("news"), 250) &"..." and the string wouldn't contain a link it would return a longer preview the preview:
On Saturday 23rd of september 2006 is the final for the 'the golden twelve' in domain Vossenberg in Hoog. Tickets can be purchased for € 85,00 per person (all-in).
'The golden twelve' are the girls that will advance to the f...

I want the preview the user sees to be of the same length for each newsitem wether it contains links/codes or not and I don't want bits of HTML at the end like in the two first examples above.
How can I achieve this?

(A plus would be if I could also end the preview after a word and not in the middle of a word like in the third example. This is not necesary, would be nice but it isn't a problem.)
0
 
LVL 4

Expert Comment

by:FSIFM
ID: 17016835
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 25

Accepted Solution

by:
kevp75 earned 100 total points
ID: 17018671
I use a function to do this for me.  The function is:
Function TruncateMemo(str, ln)
      Dim i, j, k
      If Len(str) > 0 And Len(str) > ln Then
            k = 1
            Do While k > 0 And k < Len(str)
                  i = InStr(k, str, " ", 1)
                  j = InStr(k, str, vbCrLf, 1)
                  If i < 0 And j < 0 Then ' Not able to truncate
                        TruncateMemo = str
                        Exit Function
                  Else
                        If i > 0 And j > 0 Then
                              If i < j Then
                                    k = i
                              Else
                                    k = j
                              End If
                        ElseIf i > 0 Then
                              k = i
                        ElseIf j > 0 Then
                              k = j
                        End If
                        If k >= ln Then
                              TruncateMemo = Mid(str, 1, k-1) & "  (more ...)"
                              Exit Function
                        Else
                              k = k + 1
                        End If
                  End If
            Loop
      Else
            TruncateMemo = str
      End If
End Function


to use it do:
<%=response.write(TruncateMemo(yourString,250)%>  this will truncate your string to 250 characters, if the length of the string is greater than 250 then it will display (more...) at the end of it.

HTH
0
 
LVL 25

Expert Comment

by:kevp75
ID: 17018678
sorry...there's a typo in my response.write statement.  it should be:
<%=response.write(TruncateMemo(yourString, 250))%>
0
 
LVL 2

Author Comment

by:seloh
ID: 17021150
That just works fantastic! Thanks!!
0
 
LVL 25

Expert Comment

by:kevp75
ID: 17021353
no prob.  Thanks for the grade
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now