ASP Trim Function?

J

Johnboy

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 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
 
D

dlbjr

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!
 
R

Roland Hall

in message
: 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 : 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
 
A

Agoston Bejo

Johnboy said:
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 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.
 
C

Chris Hohmann

Johnboy said:
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 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>")
%>
 
D

dlbjr

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

'dlbjr
'Pleading sagacious indoctrination!
 
D

dlbjr

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

'dlbjr
'Pleading sagacious indoctrination!
 
C

Chris Hohmann

dlbjr said:
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.
 
R

Roland Hall

in message
: : >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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top