?
Solved

truncate string

Posted on 2006-06-30
7
Medium Priority
?
897 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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 have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

850 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