Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

truncate string

Posted on 2006-06-30
7
Medium Priority
?
896 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 25

Accepted Solution

by:
kevp75 earned 400 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

610 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