Forums

ASP

This topic is locked

trimming either side of a word by 50

Posted 03 May 2005 14:00:56
1
has voted
03 May 2005 14:00:56 adam partridge posted:
how would i trim a paragraph of text 50 characters both left and right from the word ?

Replies

Replied 03 May 2005 16:03:19
03 May 2005 16:03:19 Lee Diggins replied:
Hi Adam

Something like this:

<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<%
Dim strIn, strOut, intLeft, intRight, strWord

strIn = strIn & "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Duis ligula lorem, consequat eget, tristique nec, "
strIn = strIn & "auctor quis, purus. Vivamus ut sem. Fusce aliquam nunc vitae purus. Aenean viverra malesuada libero. Fusce "
strIn = strIn & "ac quam. Donec neque. Nunc venenatis enim nec quam. Cras faucibus, justo vel accumsan aliquam, tellus dui "
strIn = strIn & "fringilla quam, in condimentum augue lorem non tellus. Pellentesque id arcu non sem placerat iaculis. Curabitur "
strIn = strIn & "posuere, pede vitae lacinia accumsan, enim nibh elementum orci, ut volutpat eros sapien nec sapien. "

intLeft = 50
intRight = 50

strWord = "Donec"

strOut = Mid(strIn, InStr(1, strIn, strWord, 1) - intLeft, intLeft + intRight + Len(strWord))
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<p><%=StrOut%></p>
</body>
</html>


Sharing Knowledge Saves Valuable Time!!!
~ ~ ~ ~ ~ ~
<b>Lee Diggins</b> - <i>DMXzone Manager</i>
&lt;font size="1"&gt;[ Studio MX/MX2004 | ASP -&gt; VBScript/PerlScript/JavaScript | SQL | CSS ]&lt;/font&gt;

Edited by - Digga the Wolf on 03 May 2005 16:20:41
Replied 03 May 2005 16:10:30
03 May 2005 16:10:30 Lee Diggins replied:
Hi Adam

If the string contains less than fifty characters to the left or right, you'll have to cater for that in your code, if you're not sure how to, post back.

Sharing Knowledge Saves Valuable Time!!!
~ ~ ~ ~ ~ ~
<b>Lee Diggins</b> - <i>DMXzone Manager</i>
<font size="1">[ Studio MX/MX2004 | ASP -> VBScript/PerlScript/JavaScript | SQL | CSS ]</font>
Replied 03 May 2005 17:46:21
03 May 2005 17:46:21 adam partridge replied:
lee thanks for hte help its much appreciated.. however little stuck on handling the returned value if its less than 50 chars at the start or the end.. other than that it works great

Replied 03 May 2005 18:07:35
03 May 2005 18:07:35 Lee Diggins replied:
Hi Adam

I'll rewrite and post back later today hopefully, if not tomorrow, bit busy at the mo.

Sharing Knowledge Saves Valuable Time!!!
~ ~ ~ ~ ~ ~
<b>Lee Diggins</b> - <i>DMXzone Manager</i>
<font size="1">[ Studio MX/MX2004 | ASP -> VBScript/PerlScript/JavaScript | SQL | CSS ]</font>
Replied 03 May 2005 18:43:19
03 May 2005 18:43:19 Lee Diggins replied:
Hi Adam

Here you go bud, should be ok, later you might want to start looking at checking for unwanted characters at both ends of the string:

&lt;%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%&gt;
&lt;%
Dim strIn, strOut, intLeft, intRight, strWord, intCheckIn, intCheckOut

'assign the string value to the strIn variable
strIn = strIn & "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Duis ligula lorem, consequat eget, tristique nec, "
strIn = strIn & "auctor quis, purus. Vivamus ut sem. Fusce aliquam nunc vitae purus. Aenean viverra malesuada libero. Fusce "
strIn = strIn & "ac quam. Donec neque. Nunc venenatis enim nec quam. Cras faucibus, justo vel accumsan aliquam, tellus dui "
strIn = strIn & "fringilla quam, in condimentum augue lorem non tellus. Pellentesque id arcu non sem placerat iaculis. Curabitur "
strIn = strIn & "posuere, pede vitae lacinia accumsan, enim nibh elementum orci, ut volutpat eros sapien nec sapien. "

'set how many characters on the left you want
intLeft = 50

'set how many characters on the right you want
intRight = 50

'set the word you're looking for
strWord = "ipsum"

'assign the intCheckIn variable the value of where in the strIn variable the first instance of the word can be found
intCheckIn = InStr(1, strIn, strWord, 1)

'check to see if there are enough available characters for the left-hand side
If intCheckIn &lt; intLeft Then
intCheckOut = 1
Else
intCheckOut = intCheckIn - intLeft
End If

'create output string
strOut = Mid(strIn, intCheckOut, intLeft + intRight + Len(strWord))
%&gt;
&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "www.w3.org/TR/html4/loose.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;p&gt;&lt;%=StrOut%&gt;&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;


Sharing Knowledge Saves Valuable Time!!!
~ ~ ~ ~ ~ ~
<b>Lee Diggins</b> - <i>DMXzone Manager</i>
<font size="1">[ Studio MX/MX2004 | ASP -> VBScript/PerlScript/JavaScript | SQL | CSS ]</font>
Replied 24 May 2005 16:42:17
24 May 2005 16:42:17 adam partridge replied:
ok i have the following page but how would i hide the recordset fields if there is no match ?

<pre id=code><font face=courier size=2 id=code>&lt;%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%&gt;
&lt;%
intLeft = 150
intRight = 150
Function getmiddle(strIn, intLeft, intRight, strWord)

'assign the intCheckIn variable the value of where in the strIn variable the first instance of the word can be found
intCheckIn = InStr(1, strIn, strWord, 1)

'check to see if there are enough available characters for the left-hand side
If intCheckIn &lt; intLeft Then
intCheckOut = 1
Else
intCheckOut = intCheckIn - intLeft
End If

'create output string
getmiddle = Mid(strIn, intCheckOut, intLeft + intRight + Len(strWord))

End Function
%&gt;
&lt;!--#include file="Connections/website.asp" --&gt;
&lt;%
Dim rssearchresults__MMColParam
rssearchresults__MMColParam = "1"
If (Request.QueryString("search" &lt;&gt; "" Then
rssearchresults__MMColParam = Request.QueryString("search"
End If
%&gt;
&lt;%
Dim rssearchresults
Dim rssearchresults_numRows

Set rssearchresults = Server.CreateObject("ADODB.Recordset"
rssearchresults.ActiveConnection = MM_website_STRING
rssearchresults.Source = "SELECT intid, strtitlemeta AS field1, strkeywords AS field2, strdescription AS field3, strpagecontent AS field4 FROM dbo.tblpages WHERE strtitlemeta LIKE '%" + Replace(rssearchresults__MMColParam, "'", "''" + "%' OR strkeywords LIKE '%" + Replace(rssearchresults__MMColParam, "'", "''" + "%' OR strdescription LIKE '%" + Replace(rssearchresults__MMColParam, "'", "''" + "%' OR strpagecontent LIKE '%" + Replace(rssearchresults__MMColParam, "'", "''" + "%' AND intactive = 1"
rssearchresults.CursorType = 0
rssearchresults.CursorLocation = 2
rssearchresults.LockType = 1
rssearchresults.Open()

rssearchresults_numRows = 0
%&gt;
&lt;%
Dim Repeat1__numRows
Dim Repeat1__index

Repeat1__numRows = -1
Repeat1__index = 0
rssearchresults_numRows = rssearchresults_numRows + Repeat1__numRows
%&gt;

&lt;%
While ((Repeat1__numRows &lt;&gt; 0) AND (NOT rssearchresults.EOF))
%&gt;

&lt;%=getmiddle(rssearchresults.Fields.Item("field1".Value, intLeft, intRight, rssearchresults__MMColParam)%&gt;one = &lt;%=(rssearchresults.Fields.Item("intid".Value)%&gt;
&lt;%=getmiddle(rssearchresults.Fields.Item("field2".Value, intLeft, intRight, rssearchresults__MMColParam)%&gt;two = &lt;%=(rssearchresults.Fields.Item("intid".Value)%&gt;
&lt;%=getmiddle(rssearchresults.Fields.Item("field3".Value, intLeft, intRight, rssearchresults__MMColParam)%&gt;three = &lt;%=(rssearchresults.Fields.Item("intid".Value)%&gt;
&lt;%=getmiddle(rssearchresults.Fields.Item("field4".Value, intLeft, intRight, rssearchresults__MMColParam)%&gt;four = &lt;%=(rssearchresults.Fields.Item("intid".Value)%&gt;

&lt;hr&gt;
&lt;%
Repeat1__index=Repeat1__index+1
Repeat1__numRows=Repeat1__numRows-1
rssearchresults.MoveNext()
Wend
%&gt;

&lt;%
rssearchresults.Close()
Set rssearchresults = Nothing
%&gt;
</font id=code></pre id=code>

Replied 24 May 2005 17:37:32
24 May 2005 17:37:32 Lee Diggins replied:
Matching what Adam?

Sharing Knowledge Saves Valuable Time!!!
~ ~ ~ ~ ~ ~
<b>Lee Diggins</b> - <i>DMXzone Manager</i>
<font size="1">[ Studio MX/MX2004 | ASP -> VBScript/PerlScript/JavaScript | SQL | CSS ]</font>
Replied 24 May 2005 17:42:41
24 May 2005 17:42:41 adam partridge replied:
well the SQL query returns rows that contain four fields, either of those columns field1, field2, field3 could contain the searched for word .. what i want to do is hide the the field on the display page if the word isnt contained within it. if that makes sense ?

Replied 24 May 2005 18:20:40
24 May 2005 18:20:40 Lee Diggins replied:
You need to add something like:
If inStr(1, rssearchresults.Fields.Item("field1".Value, rssearchresults__MMColParam, 1) &gt; 0 Then
getmiddle(rssearchresults.Fields.Item("field1".Value, intLeft, intRight, rssearchresults__MMColParam)
End If

Sharing Knowledge Saves Valuable Time!!!
~ ~ ~ ~ ~ ~
<b>Lee Diggins</b> - <i>DMXzone Manager</i>
<font size="1">[ Studio MX/MX2004 | ASP -> VBScript/PerlScript/JavaScript | SQL | CSS ]</font>
Replied 24 May 2005 18:22:14
24 May 2005 18:22:14 adam partridge replied:
would this work if i added it to the function ?

Replied 24 May 2005 18:55:44
24 May 2005 18:55:44 Lee Diggins replied:
You could write the checking in the function, you need to remember what you want to display if there isn't a match, do you still want One = etc if the entry is blank as there won't be any returned text from the function.

Maybe try and outline exactly what you want to see when certain criteria are met and when they're not, then a little re-write should sort it.

Post back, I'll ceck in an hour or so.

Sharing Knowledge Saves Valuable Time!!!
~ ~ ~ ~ ~ ~
<b>Lee Diggins</b> - <i>DMXzone Manager</i>
<font size="1">[ Studio MX/MX2004 | ASP -> VBScript/PerlScript/JavaScript | SQL | CSS ]</font>
Replied 24 May 2005 19:20:38
24 May 2005 19:20:38 adam partridge replied:
lee thanks for your help its much appreciated your a star.. gonna leave itfor today come back tomorow with a fresh head ill let u know how i get on

once again thanxz

Reply to this topic