Solved

truncate string

Posted on 2006-06-30
7
890 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
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…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

777 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