PDA

View Full Version : something from the middle!


Boxhead
01-09-2004, 03:20 PM
I want to get the id from the middle of a string like this:

<img src="/images/link_ext.gif" alt="External Site Link" width="13" height="13" align="absmiddle"> <a class="news" href="/links/external.asp?id=208">Kidscape</a>

The trouble is it may be in a different place each time so I cant use mid.

What can I do?

monkey

A1ien51
01-09-2004, 03:25 PM
you want the query string id??

Request.QueryString("XXXX")

?????

Eric

Boxhead
01-09-2004, 03:27 PM
No - the string comes from a database field:

(Recordset1.Fields.Item("n_related_links").Value)

monkey

A1ien51
01-09-2004, 03:38 PM
I am not sure if I am wake yet or not, but I am not sure what you are trying to do exactly....

When are you trying to get the id?

If you are using Recordset1.Fields.Item("n_related_links").Value, don't you already know what the value is???

Boxhead
01-09-2004, 03:54 PM
It's a friday, we're all very tired!

Recordset1.Fields.Item("n_related_links").Value

this field contains text such as:

<img src="/images/link_ext.gif" alt="External Site Link" width="13" height="13" align="absmiddle"> <a class="news" href="/links/external.asp?id=208">Kidscape</a>

From this text I want to take the id from the end of the url - ie 208

The information is used on a website, and the link takes the user to a page with an external link and a disclaimer about external sites not being our responsibility. i want to use this infomation on a different site, but my external link page is different so I just need the id from this string.

Hope this is a bit clearer - just image how complex this is for me!!

Agggghhhhhh!

Monkey:confused:

A1ien51
01-09-2004, 04:02 PM
So you are saying that whole line is in the database....

I have to think about my reg. exps now....


I will see if I can come up with something....

A1ien51
01-09-2004, 04:10 PM
This just came off the top of my head, not sure if it will work, but it is worth a shot. I am sure someone else can come up with something a lot nicer.

Not Tested on 1 cup of coffee....

dim arrStr1
dim arrStr2
dim theStr
dim idNum
theStr = Recordset1.Fields.Item("n_related_links").Value
arrStr1 = Split(theStr,"id=")
arrStr2 = Split(arrStr1(1),""">")
idNum = arrStr2(0)

Boxhead
01-09-2004, 05:23 PM
OK - this is throwing up errors, so I think I need to give the full picture. here is the sort of string I will be getting:

<img src="/images/link_ext.gif" alt="External Site Link" width="13" height="13" align="absmiddle"><a href="/links/external.asp?id=208">Kidscape</a><br>
<img src="/images/link_ext.gif" alt="External Site Link" width="13" height="13" align="absmiddle"><a href="/links/external.asp?id=219">Childnet International</a> Email: <a href="mailto:info@childnet-int.org">info@childnet-int.org</a>)<br>
<img src="/images/link_ext.gif" alt="External Site Link" width="13" height="13" align="absmiddle"> <a href="/links/external.asp?id=89">ChatDanger</a><br>
<img src="/images/link_ext.gif" alt="External Site Link" width="13" height="13" align="absmiddle"> <a href="/links/external.asp?id=220">National Society for the Prevention of Cruelty to Children</a> (Email: <a href="mailto:help@nspcc.org.uk">help@nspcc.org.uk</a>br>
<img src="/images/link_ext.gif" alt="External Site Link" width="13" height="13" align="absmiddle"> <a href="/links/external.asp?id=186">Bullying Online</a>

I need to do the following:

change the img src to /assets.
change the href's to javascript calls (so I cant do a replace because I need to change the other end from "> to ')">.

so far I have:

<%
Dim relLinks, lnkArray, lnkALen
//gets the field and replaces the /images with ../assets
relLinks = Replace(Recordset1.Fields.Item("n_related_links").Value,"src=""/images/","src=""../assets/")
//splits rellink into seperate links
lnkArray=split(relLinks,"<img src=",-1,1)
lnkALen = UBound(lnkArray)
i=0
//this loop adds the <img src= back on each link
While (i<=lnkALen)
thisArray = lnkArray(i)
If (i>0) Then
thisArray ="<img src=" + thisArray
End If
Dim arrStrA, arrStrB, idNum
arrStrA = split(thisArray,"?id=",-1,1)
arrStrB = Split(arrStrA(1),""">")
idNum = arrStr2(0)
//this bit is erroring (An unhandled data type was encountered.) After I have found the id I will replace the href and put my javascript in with the id added
i=i+1
Wend

%>

jim_denney
01-09-2004, 11:15 PM
You could use the vbScript InStr() function to locate the start of the id. It looks like the ID always starts with "id=", so place the link address in a string and try to locate the id:

InStr(add_str, "id=")

From there you could use the Mid function to grab as many bytes as needed. Then search for the double quotes coming directly after the ID number to find where the ID number ends, also using InStr.

glenngv
01-12-2004, 10:30 AM
Why did you store such kind of data in the first place?
You should have stored the needed items separately, something like this:

LinkImagePath | LinkId | LinkName
=====================================
images | 208 | Kidscape
images | 219 | Childnet International
images | 89 | ChatDanger


With these kind of data, you don't need to extract them from long html data.

Boxhead
01-12-2004, 10:58 AM
Glenngv

agree totally!! Unfortunately this is one for the future, as we are changing our CMS to use a rich text editor, abad want to wait until then to see our options!

Cheers for highlighting a problem

monkey

glenngv
01-13-2004, 02:37 AM
ok, let's try this..
Since each link is separated b <br>s, you split the whole string with <br>

theStr = Recordset1.Fields.Item("n_related_links").Value
arrStr1 = Split(theStr,"<br>")

Then loop thru each item in the array to extract the id, change the src and href.

Boxhead
01-13-2004, 10:25 AM
sorry Glen - sorted this yesterday - here was my final code if you fancy a look:

<%
'get links, spilt into ext or int and change the href to javascript on the ext
Dim relLinks, lnkArray, lnkALen, arrStrA, arrStrB, idNum
'get links and replace /images with ../assets
relLinks = Replace(Recordset1.Fields.Item("n_related_links").Value,"src=""/images/","src=""../assets/")
'Is there links?
If relLinks <> "-" Then
Response.Write("<p><strong>Related Links:</strong></p>")
'creates array of each link and find the length of the array
lnkArray=split(relLinks,"<img src=",-1,1)
lnkALen = UBound(lnkArray)
i=1
While (i<=lnkALen)
thisArray = lnkArray(i)
'add <imgsrc= back the each array element and finds is the element is internal or external (linkType returns 0 if internal)
thisArray ="<img src=" + thisArray
linkType = InStr(thisArray,"link_ext")
If (linkType > 0) Then
idPos = InStr (thisArray,"id=") ' finds the position of the id
idNum = mid(thisArray,idPos,8) ' takes the id out of element (+ a few extra characters)
charNo = (InStr(idNum,"""")-4) ' finds out how many numbers are in the element (position of " - 4("id=" +1 for "))
idNum = Mid(idNum,4,charNo) ' gets id out
link = Replace(thisArray, "href=""/links/external.asp?id="&idNum&"""", "href=javascript:card_BrWindow('../contact_card.asp?id="&idNum&"')")
Response.Write(link)
Else
Response.Write(thisArray)
End If
i=i+1
Wend
End If
%>

What do you think (i'm still new to vbscript?)

monkey

glenngv
01-13-2004, 10:34 AM
You have shown a sample string retrieved from the database. Could you please show the expected output of that string after doing a couple of replacements. That way I can clearly see what you're trying to accomplish. By then, I could probably offer a possible enhancement of your code, if needed.

Boxhead
01-13-2004, 10:37 AM
This is the string that is served from the original string posted on this thread -

<p><strong>Related Links:</strong></p><img src="../assets/link_ext.gif" alt="External Site Link" width="13" height="13" align="absmiddle"> <a class="news" href=javascript:card_BrWindow('../contact_card.asp?id=208')>Kidscape</a><br>
<img src="../assets/link_ext.gif" alt="External Site Link" width="13" height="13" align="absmiddle"> <a class="news" href=javascript:card_BrWindow('../contact_card.asp?id=219')>Childnet International</a> <span class="news">(Email: <a class="news" href="mailto:info@childnet-int.org">info@childnet-int.org</a>)</span><br>
<img src="../assets/link_ext.gif" alt="External Site Link" width="13" height="13" align="absmiddle"> <a class="news" href=javascript:card_BrWindow('../contact_card.asp?id=89')>ChatDanger</a><br>
<img src="../assets/link_ext.gif" alt="External Site Link" width="13" height="13" align="absmiddle"> <a class="news" href=javascript:card_BrWindow('../contact_card.asp?id=220')>National Society for the Prevention of Cruelty to Children</a> <span class="news">(Email: <a class="news" href="mailto:help@nspcc.org.uk">help@nspcc.org.uk</a>)</span><br>
<img src="../assets/link_ext.gif" alt="External Site Link" width="13" height="13" align="absmiddle"> <a class="news" href=javascript:card_BrWindow('../contact_card.asp?id=186')>Bullying Online</a>

glenngv
01-14-2004, 03:23 AM
arrLine = split(Recordset1.Fields.Item("n_related_links").Value,"<br>") 'split each link using <br>
for i = 0 to ubound(arrLine)
line = replace(arrLine(i),"/images/","../assets/")
idpos1 = instr(line,"id=") 'start position of id
line2 = mid(line,idpos1)
idpos2 = instr(line2,""">") 'end position of id with reference from idpos1
id = mid(line,idpos1+3,idpos2-4) 'get id
line = replace(line,"/links/external.asp?id="&id,"javascript:card_BrWindow('../contact_card.asp?id="&id&"')")
response.write line & "<br>"
next
%>


I just put the basic logic and left out the necessary condition checkings.
And I changed br> with <br> in the original string, probably it was just a typo(?)
If not and you aren't sure that the strings (links) in the db are always separated by <br>s, then my code will not work.

Boxhead
01-14-2004, 12:27 PM
Glen

This seems a cleaner way of doing things, especially the For statement and the split by <br>. One more question, do I need to declare the variables at the start ie

Dim idpos1, idpos2 etc

monkey

glenngv
01-15-2004, 06:36 AM
Of course. It's always advisable to always declare your variables.