ASP Trim Function?

Discussion in 'ASP General' started by Johnboy, May 13, 2005.

  1. Johnboy

    Johnboy Guest

    Hi,
    I'm creating a website that will have a page that lists current news
    items. The news items will be stored in a SQL Server database and will
    have basically two fields, the date and the news blurb . . . On the
    main page of the site, I want to have a box that shows "Current News"
    and only pulls the first 7 or so words from the news blurb followed by
    a ... [more] where [more] is a link to the current news page.

    For example, the database entry may be something like this:

    5/12/05
    Chicken Little reports sky is falling. Earlier today Mr. Little was
    struck in the head by an unidentified object and immediately announced
    the sky was falling.

    So on the main page of the of the site it would say:

    Current News:
    5/12/05 Chicken Little reports sky is falling. Earlier ... [more]

    Is there some sort of Trim function in ASP that I could use to count
    the number of spaces in an article and if Less than 7 print the whole
    thing, or if more than 7 trim all characters after the 7th space?

    Any info would be greatly appreciated.

    Thanks!
    John
     
    Johnboy, May 13, 2005
    #1
    1. Advertising

  2. Johnboy

    dlbjr Guest

    Dim ar2(6)
    'Place text into an array
    ar1 = Split(strText," ")
    intMax = UBound(ar1)
    If intMax > 6 Then
    intMax = 6
    End If
    For i = 0 To intMax
    ar2(i) = ar1(i)
    Next
    strNewString = Join(ar2," ") & "... [more] "

    'dlbjr
    'Pleading sagacious indoctrination!
     
    dlbjr, May 13, 2005
    #2
    1. Advertising

  3. Johnboy

    Roland Hall Guest

    "Johnboy" wrote in message
    news:...
    : I'm creating a website that will have a page that lists current news
    : items. The news items will be stored in a SQL Server database and will
    : have basically two fields, the date and the news blurb . . . On the
    : main page of the site, I want to have a box that shows "Current News"
    : and only pulls the first 7 or so words from the news blurb followed by
    : a ... [more] where [more] is a link to the current news page.
    :
    : For example, the database entry may be something like this:
    :
    : 5/12/05
    : Chicken Little reports sky is falling. Earlier today Mr. Little was
    : struck in the head by an unidentified object and immediately announced
    : the sky was falling.
    :
    : So on the main page of the of the site it would say:
    :
    : Current News:
    : 5/12/05 Chicken Little reports sky is falling. Earlier ... [more]
    :
    : Is there some sort of Trim function in ASP that I could use to count
    : the number of spaces in an article and if Less than 7 print the whole
    : thing, or if more than 7 trim all characters after the 7th space?
    :
    : Any info would be greatly appreciated.

    My little version... (the last line is pretty long)

    <%@ Language=VBScript %>
    <%
    Option Explicit
    Response.Buffer = True

    function getPos(d, a)
    dim char, count, pos
    char = chr(32) : count = 0
    pos = instr(1, article, char)
    do until count = 7
    count = count + 1
    if pos = 0 then
    exit do
    else
    pos = instr(pos + 1, article, char)
    end if
    loop
    getPos = pos
    end function

    dim articleDate, article, p, preview, fullview
    articleDate = #5/12/05#
    article = "Chicken Little reports sky is falling. Earlier today Mr. Little
    was struck in the head by an unidentified object and immediately announced
    the sky was falling."

    p = getPos(articleDate, article)

    if p = 0 then
    Response.Write articleDate & " " & article
    else
    preview = left(article,p)
    fullview = mid(article,p)
    Response.Write articleDate & " " & left(article,p) & "<span
    id=""ellipse""> ... </span><span id=""article"" style=""cursor: pointer;
    color: blue"" onmouseover=""this.style.backgroundColor='#eef'""
    onmouseout=""this.style.backgroundColor='#fff'""
    onclick=""document.getElementById('ellipse').innerHTML='';
    this.style.cursor='text'; onmouseover=''; this.style.color='#000';
    this.innerHTML='" & fullview & "'"">[more]</span>"
    end if
    %>

    http://kiddanger.com/lab/morearticle.asp

    --
    Roland Hall
    /* This information is distributed in the hope that it will be useful, but
    without any warranty; without even the implied warranty of merchantability
    or fitness for a particular purpose. */
    Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
    WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
    MSDN Library - http://msdn.microsoft.com/library/default.asp
     
    Roland Hall, May 13, 2005
    #3
  4. Johnboy

    Agoston Bejo Guest

    "Johnboy" <> wrote in message
    news:...
    > Hi,
    > I'm creating a website that will have a page that lists current news
    > items. The news items will be stored in a SQL Server database and will
    > have basically two fields, the date and the news blurb . . . On the
    > main page of the site, I want to have a box that shows "Current News"
    > and only pulls the first 7 or so words from the news blurb followed by
    > a ... [more] where [more] is a link to the current news page.
    >
    > For example, the database entry may be something like this:
    >
    > 5/12/05
    > Chicken Little reports sky is falling. Earlier today Mr. Little was
    > struck in the head by an unidentified object and immediately announced
    > the sky was falling.
    >
    > So on the main page of the of the site it would say:
    >
    > Current News:
    > 5/12/05 Chicken Little reports sky is falling. Earlier ... [more]
    >
    > Is there some sort of Trim function in ASP that I could use to count
    > the number of spaces in an article and if Less than 7 print the whole
    > thing, or if more than 7 trim all characters after the 7th space?


    Dim words, nWords
    words = Split(myText)
    nWords = UBound(words)
    ReDim words(7)
    Response.Write Join(words)
    If nWords >= 7 Then
    Response.Write "... [more]"
    End If

    Although if you would like to consider multiple spaces or whitespaces as one
    space, you're going to have to convert the original string so that it
    contains only one space between each word.

    >
    > Any info would be greatly appreciated.
    >
    > Thanks!
    > John
    >
     
    Agoston Bejo, May 13, 2005
    #4
  5. "Johnboy" <> wrote in message
    news:...
    > Hi,
    > I'm creating a website that will have a page that lists current news
    > items. The news items will be stored in a SQL Server database and will
    > have basically two fields, the date and the news blurb . . . On the
    > main page of the site, I want to have a box that shows "Current News"
    > and only pulls the first 7 or so words from the news blurb followed by
    > a ... [more] where [more] is a link to the current news page.
    >
    > For example, the database entry may be something like this:
    >
    > 5/12/05
    > Chicken Little reports sky is falling. Earlier today Mr. Little was
    > struck in the head by an unidentified object and immediately announced
    > the sky was falling.
    >
    > So on the main page of the of the site it would say:
    >
    > Current News:
    > 5/12/05 Chicken Little reports sky is falling. Earlier ... [more]
    >
    > Is there some sort of Trim function in ASP that I could use to count
    > the number of spaces in an article and if Less than 7 print the whole
    > thing, or if more than 7 trim all characters after the 7th space?
    >
    > Any info would be greatly appreciated.
    >
    > Thanks!
    > John
    >

    <%
    Dim s, re
    s = "Chicken Little reports sky is falling. Earlier today Mr. Little was
    struck in the head by an unidentified object and immediately announced the
    sky was falling."
    Set re = New RegExp
    re.Pattern = "((\S+\s+){7})(.+)"
    Response.Write re.Replace(s,"$1...<a href=""#"">[more]</a>")
    %>
     
    Chris Hohmann, May 15, 2005
    #5
  6. Johnboy

    dlbjr Guest

    This will fail if the string has less than 7 spaces available.

    'dlbjr
    'Pleading sagacious indoctrination!
     
    dlbjr, May 15, 2005
    #6
  7. "dlbjr" <> wrote in message
    news:%...
    > This will fail if the string has less than 7 spaces available.
    >
    > 'dlbjr
    > 'Pleading sagacious indoctrination!
    >
    >

    Why not test it and see?
     
    Chris Hohmann, May 16, 2005
    #7
  8. Johnboy

    dlbjr Guest

    I did!. It will not append the needed anchor, because the position is not found..

    'dlbjr
    'Pleading sagacious indoctrination!
     
    dlbjr, May 16, 2005
    #8
  9. "dlbjr" <> wrote in message
    news:...
    >I did!. It will not append the needed anchor, because the position is not
    >found..
    >
    > 'dlbjr
    > 'Pleading sagacious indoctrination!
    >
    >

    Why would you need an anchor if the entirety of the article was listed on
    the page? I took the OP to mean that if the entire article was seven (7)
    words or less, print it. Else, print the first seven word and a more link.
    That's what I got out of the last paragraph in the original post. If he/she
    wants a more link for every blurb, regardless of length, he/she can do this:

    <%
    Dim s1, s2, re
    s1 = "Chicken Little reports sky is falling. Earlier today Mr. Little was
    struck in the head by an unidentified object and immediately announced the
    sky was falling."
    s2 = "Very short article."
    Set re = New RegExp
    re.Pattern = "((\S+\s+){7})(.+)"
    Response.Write re.Replace(s1,"$1") & "...<a href=""#"">[more]</a><br>"
    Response.Write re.Replace(s2,"$1") & "...<a href=""#"">[more]</a><br>"
    %>

    In either case, I'm not sure the word "fail" was appropriate here. We simply
    had differing interpretations of the requirements.
     
    Chris Hohmann, May 16, 2005
    #9
  10. Johnboy

    Roland Hall Guest

    "Chris Hohmann" wrote in message
    news:...
    : "dlbjr" <> wrote in message
    : news:...
    : >I did!. It will not append the needed anchor, because the position is not
    : >found..
    : >
    : > 'dlbjr
    : > 'Pleading sagacious indoctrination!
    : >
    : >
    : Why would you need an anchor if the entirety of the article was listed on
    : the page? I took the OP to mean that if the entire article was seven (7)
    : words or less, print it. Else, print the first seven word and a more link.
    : That's what I got out of the last paragraph in the original post. If
    he/she
    : wants a more link for every blurb, regardless of length, he/she can do
    this:
    :
    : <%
    : Dim s1, s2, re
    : s1 = "Chicken Little reports sky is falling. Earlier today Mr. Little was
    : struck in the head by an unidentified object and immediately announced the
    : sky was falling."
    : s2 = "Very short article."
    : Set re = New RegExp
    : re.Pattern = "((\S+\s+){7})(.+)"
    : Response.Write re.Replace(s1,"$1") & "...<a href=""#"">[more]</a><br>"
    : Response.Write re.Replace(s2,"$1") & "...<a href=""#"">[more]</a><br>"
    : %>
    :
    : In either case, I'm not sure the word "fail" was appropriate here. We
    simply
    : had differing interpretations of the requirements.

    Nice Chris. A little modification so it just expands the text...

    Dim s, re
    s = "Chicken Little reports sky is falling. Earlier today Mr. Little was
    struck in the head by an unidentified object and immediately announced the
    sky was falling."
    Set re = New RegExp
    re.Pattern = "((\S+\s+){7})(.+)"
    Response.Write re.Replace(s,"$1<span onclick=""this.innerHTML='$3'"">
    ....[more]</span>")

    --
    Roland Hall
    /* This information is distributed in the hope that it will be useful, but
    without any warranty; without even the implied warranty of merchantability
    or fitness for a particular purpose. */
    Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
    WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
    MSDN Library - http://msdn.microsoft.com/library/default.asp
     
    Roland Hall, May 16, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. FAQ server
    Replies:
    0
    Views:
    150
    FAQ server
    Aug 29, 2006
  2. FAQ server
    Replies:
    0
    Views:
    148
    FAQ server
    Oct 26, 2006
  3. FAQ server
    Replies:
    6
    Views:
    225
    Jonas Raoni
    Dec 25, 2006
  4. FAQ server
    Replies:
    26
    Views:
    315
    Dr J R Stockton
    Feb 26, 2007
  5. FAQ server
    Replies:
    2
    Views:
    139
    -Lost
    Apr 24, 2007
Loading...

Share This Page