Forum Moderators: open

Message Too Old, No Replies

replace(str,) function

... how to detect

         

SuzyUK

6:55 pm on Dec 17, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm writing a replace function for text coming from a database trying to making it foolproof for end users ;)

However I'm trying to replace ... with … because the validator threw this up as an character not recognised

when I try to replace it within the function using:

str=replace(str, "...", "…")

it's not detecting it, function is working fine with everything else

... is being used quite a lot in the "teaser" heading leading to the full document, sometimes no space between the last character before the leader, sometimes there is...

any ideas?
Suzy

aspdaddy

7:35 pm on Dec 17, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



are you sure its not working? :)

str = "…"
Response.Write str

chameleon

7:51 pm on Dec 17, 2002 (gmt 0)

10+ Year Member



Suzie,

If the code you've listed is correct, the reason your replace isn't working is because it's searching for three periods instead of the '…' character (ASCII 133).

Try:


str=replace(str, "…", "…")

SuzyUK

8:44 pm on Dec 17, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm sure ;)

and I want it to search for three periods because that is what is being entered by the user into the database

btw it is displaying correctly, at least in my browsers, but it's just the various validators are picking it up as being incorrect..so if I searched on your character chameleon, would that not replace other "faulty" characters too? See example code below...

OK now I've got another problem too..and that is how to check for a null string:

here's a shortened version of my function, I've tried using
IF str="" THEN
db_replace_text=""
exit function
END IF

but am really not sure about the correct syntax..any help I'm about all googled out!

example function:
function db_text_replace(str)
str=replace(str, "&", "&")
str=replace(str, "'", "'")
str=replace(str, """", """)
str=replace(str, "...", "…") 'this is not working
str=replace(str, vbcrlf, "<br />" & vbcrlf)
db_text_replace=str
end function

then calling function example
article = db_text_replace(objRS("articletext"))

can I check for a null, within the function without adding the check to each variable

Suzy

[edit] grammar [/edit]

RossWal

9:56 pm on Dec 17, 2002 (gmt 0)

10+ Year Member



Suzy,
I'm not sure I understand your problem. If it is that the data field on the DB can contain nulls, and said nulls is causing problems in the call to your function or in calling the Replace function, here's a quick & dirty fix.

Make your call like this:
article = db_text_replace(objRS("articletext") & "")

What this does is concatenate an empty string onto the null value, changing it from the flaky and unstable null to a upstanding and well-behaved empty string.

SuzyUK

10:13 pm on Dec 17, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Rosswal...you're a star!

this works, now am I allowed to ask why? ;)

Suzy

[edit] I've read your post again and it explains, sorry! although it may take me all my time to get my head around it ;)[/edit]

SuzyUK

12:28 am on Dec 18, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can I now narrow it down even further thanks to your advice, Rosswal

instead of:


Make your call like this:
article = db_text_replace(objRS("articletext") & "")

My suggestion:
put this in the first line of the function itself
str="" & str
then carry on as per
...etc...str=replace(str, "&", "&amp;")

I think this does the same thing as you're saying, but you don't need to remember to put that extra bit in the call to function so the call can remain:
article=db_text_replace(objRS("articletext"))

or whatever it is you want to replace...Which would be really useful when I want to use this piece of code again....

Thanks for help
Suzy

tomasz

5:16 am on Dec 18, 2002 (gmt 0)

10+ Year Member



the SQL has function which returns user define value it works in SQL an Access. I think you can define it in your select statement and it is easier to handle than vb function.

Select isnull(myfield,'') as myfield from mytable

it will return '' value if your field is null.

SuzyUK

10:05 am on Dec 18, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



thanks tomasz, i didn't know that either, but then I don't much SQL (yet!)

(although the ... replace still isn't working I'll live with it!)

One more question..is it possible for a function to be too long? or I've added the following into the replace function above to allow non-html users to input links into their text

str=replace(str, "[url]", "<a target=""_blank"" href=""")
str=replace(str, "[/url]", """>")
str=replace(str, "[link]", "")
str=replace(str, "[/link]", "</a>")

My test server is replacing it correctly but when I uploaded it's not doing this bit.. but it is doing the rest?

is there an error in this that my PWS is being lenient with?

Suzy

[added]
on further investigation, only some of the replaces are working online... mystified

anyone know a better way to write this type of function
any advice appreciated
[/added]

wardbekker

1:02 pm on Dec 18, 2002 (gmt 0)

10+ Year Member



Suzy,

Can you specify which ones ain't working?

SuzyUK

5:28 pm on Dec 18, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Wardbekker, thanks..it's OK now

I don't know where I was going wrong, I stripped the function and added things in again one by one, only this time I discovered how to do the "character" search properly (Thanks Chameleon, I understand now;)). I was quoting them like text oops! so now I've fixed the ... dot leader problem too :)

I also changed the replace [url] [link] etc... and used the characters for the brackets in the search...It now seems to be working perfectly so I can use this function against anything pulled from the database, and I can just add things into the function as and when the validator throws up any errors..not normally so picky but this is a special case site

I'm happy, thanks to all who got me pointed in the right direction!

Suzy
:)