NewtoAllThis
asked on
Strip HTML tags from data in database rows
Hi,
I'm trying to do something like this, but I need help.
I have a table which contains Adverts for a magazine, there is a column cotaining Ad Headings. I have also stored HTML tags around the ad headings in the table. Each advert is displayed on many pages on my site.
But I want to display an ad on some pages without the HTML tags. How do I strip the html tags from the database for this page without changing the ad in each other page.
******All my code is ASP and javascript.*****
This is my code that outputs the adverts, where html tags are included.
r1 = Q("AdHead"); //Here r1 stores the ad heading with html tags
Response.Write("<td valign='top' width=400 rowspan=2>"+r1+"</td>"); //Header ok but tags included
But when I do this
r1 = Q("AdHead");
r1 = Strip(r1); //I need a strip html javascript function here
Response.Write("<td valign='top' width=400 rowspan=2>"+r1+"</td>"); //Header
Hope this makes sense.
Thanks
Crap! This guy beat me to it while I was created the regular expression!! I don't know if his or my solution will work in all circumstances but here goes.
function Strip(str) {
return str.replace(/<[\w/]+[^<>]* >/g, "");
}
function Strip(str) {
return str.replace(/<[\w/]+[^<>]*
}
ASKER
I've tried both functions and I'm getting the following error.
Microsoft JScript runtime error '800a138f'
Object expected
?
Microsoft JScript runtime error '800a138f'
Object expected
?
ASKER
Am I calling the function correctly. Here is my code
<%
.
.
r1 = RS("AdHead");
r2 = strip(r1);
Response.Write("<td valign='top' width=400 rowspan=2>"+r2+"</td>");
.
.
.
%>
With this code I'm getting the error in the above comment
Thanks
<%
.
.
r1 = RS("AdHead");
r2 = strip(r1);
Response.Write("<td valign='top' width=400 rowspan=2>"+r2+"</td>");
.
.
.
%>
With this code I'm getting the error in the above comment
Thanks
I might have made a mistake in the reg exp, try this simpler one.
function Strip(str) {
return str.replace(/<[^>]*>/g, "");
}
function Strip(str) {
return str.replace(/<[^>]*>/g, "");
}
oops...that was kind of sloppy of me. I'm too used to C/C++
This function works and was tested
Call it like this in your code:
r1 = RS("AdHead");
r2 = stripHTML(r1);
Response.Write("<td valign='top' width=400 rowspan=2>"+r2+"</td>");
This is the new function
function stripHTML( oldString )
{
var newString = "";
var inTag = false;
for( var i = 0; i < oldString.length; i++ )
{
if( oldString.charAt(i) == '<' ) inTag = true;
if( oldString.charAt(i) == '>' ) {
inTag = false;
i++;
}
if( !inTag ) newString += oldString.charAt(i);
}
return newString;
}
This function works and was tested
Call it like this in your code:
r1 = RS("AdHead");
r2 = stripHTML(r1);
Response.Write("<td valign='top' width=400 rowspan=2>"+r2+"</td>");
This is the new function
function stripHTML( oldString )
{
var newString = "";
var inTag = false;
for( var i = 0; i < oldString.length; i++ )
{
if( oldString.charAt(i) == '<' ) inTag = true;
if( oldString.charAt(i) == '>' ) {
inTag = false;
i++;
}
if( !inTag ) newString += oldString.charAt(i);
}
return newString;
}
ASKER
I'm still getting object expected error.
This is the function code within HTML <head>
<script language="JavaScript" type="text/javascript">
<!--
function stripHTML( oldString )
{
var newString = "";
var inTag = false;
for( var i = 0; i < oldString.length; i++ )
{
if( oldString.charAt(i) == '<' ) inTag = true;
if( oldString.charAt(i) == '>' ) {
inTag = false;
i++;
}
if( !inTag ) newString += oldString.charAt(i);
}
return newString;
}
//-->
</script>
And then I'm calling it like so inside a table within ASP tags
<%
.
.
r1 = Q("AdHead");
r2 = stripHTML(r1); //If I comment out this line I dont get an error but tags are still included.So I'm guessing it's the way the function is being called ??
Response.Write("<td>"+r1+" </td>");
.
%>
THANKS FOR ALL THE HELP SO FAR :D
This is the function code within HTML <head>
<script language="JavaScript" type="text/javascript">
<!--
function stripHTML( oldString )
{
var newString = "";
var inTag = false;
for( var i = 0; i < oldString.length; i++ )
{
if( oldString.charAt(i) == '<' ) inTag = true;
if( oldString.charAt(i) == '>' ) {
inTag = false;
i++;
}
if( !inTag ) newString += oldString.charAt(i);
}
return newString;
}
//-->
</script>
And then I'm calling it like so inside a table within ASP tags
<%
.
.
r1 = Q("AdHead");
r2 = stripHTML(r1); //If I comment out this line I dont get an error but tags are still included.So I'm guessing it's the way the function is being called ??
Response.Write("<td>"+r1+"
.
%>
THANKS FOR ALL THE HELP SO FAR :D
ah mate, you can't mix client-side and server-side code like that. The server-side code is executed before the client-side code is even seen. Put the JS function in your server-side code.
ASKER
Ok as I'm sure you can guess by my name I'm new to this ASP, javascript stuff. I've been messing around with it but getting now where
Here is my entire page of code can you show me where I should call the function.
Cheers
<%@ Language = "JavaScript" %>
<html>
<head>
<!-- #include file="../include/ado.inc" -->
<!-- #include file="../include/filters.a sp" -->
<meta http-equiv="Content-Langua ge" content="en-au">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>StoreConnect Search</title>
<link href="ExecutionStyles.css" rel="stylesheet" type="text/css">
<style type="text/css">
<!--
.flat {BORDER-RIGHT: #003399 1px solid; BORDER-TOP: #003399 1px solid; BORDER-LEFT: #003399 1px solid; BORDER-BOTTOM: #003399 1px solid; text-decoration: none}
.flatbutton {BORDER-RIGHT: #666666 1px solid; BORDER-TOP: #666666 1px solid; BORDER-LEFT: #666666 1px solid; BORDER-BOTTOM: #666666 1px solid}
.topl {BORDER-TOP: gray 1px solid}
.botl {BORDER-BOTTOM: gray 1px solid;font-family: Arial, Helvetica, sans-serif; font-size: 12px; color: #000000; text-align: left;}
.botsl {BORDER-BOTTOM: silver 1px solid;font-family: Arial, Helvetica, sans-serif; font-size: 12px; color: #000000; text-align: left;}
.norm {font-family: Verdana,Arial, Helvetica, sans-serif; font-size: 15px; color: #000000; text-align: left;}
.smtxt {font-family: Verdana,Arial, Helvetica, sans-serif; font-size: 13px; line-height: 15px; color: #000000; text-align: left;}
.vsmtxt {font-family: Verdana,Arial, Helvetica, sans-serif; font-size: 10px; line-height: 15px; color: #000000; text-align: left; text-decoration: none}
.hline {font-family: Verdana,Arial, Helvetica, sans-serif; font-size: 12px; color: #000000; text-align: left;}
//-->
</style>
<script language="JavaScript" type="text/javascript">
<!--
function stripHTML( oldString )
{
var newString = "";
var inTag = false;
for( var i = 0; i < oldString.length; i++ )
{
if( oldString.charAt(i) == '<' ) inTag = true;
if( oldString.charAt(i) == '>' ) {
inTag = false;
i++;
}
if( !inTag ) newString += oldString.charAt(i);
}
return newString;
}
//-->
</script>
</head>
<body TOPMARGIN="0" LEFTMARGIN="0" MARGINWIDTH="0" MARGINHEIGHT="0"">
<%
var Move;
var PageNo;
var TheEnd;
Move = Request("Move");
PageNo = Request("PageNo");
if (!(PageNo > 0 || PageNo < 999)) {
PageNo = 1;
TheEnd = 0;
}
var Q;
Q = Server.CreateObject("ADODB .Recordset ");
strSQL = "select Retailer, Brand, ConnectCode, Min(RetailerLogo) as RTlogo, Min(AdHeader) as AdHead, Min(AdImage) as AdImg, Min(MajorChainLogo) as MCLogo, Min(MCName) as MCN from ADRetailersV "
strSQL += " where mcid = '"+Request("ID")+"'";
strSQL += " group by Retailer, Brand, ConnectCode";
strSQL += " order by Retailer";
Q.Open(strSQL, Conn, adOpenStatic);
Q.PageSize = 2;
if (!Q.EOF) {
if (Move == "Previous") {
if (PageNo > 1) {
PageNo--;
} else {
PageNo = 1;
}
}
else if (Move == "Next") {
if (Q.AbsolutePage < Q.PageCount) {
PageNo++;
} else {
PageNo = Q.PageCount;
}
}
else {
PageNo = 1;
}
Q.AbsolutePage = PageNo;
} else
{
TheEnd = 1;
}
%>
<!--Promos table-->
<table width="770" cellspacing=0 cellpadding=0 border=1 class="norm" valign="center">
<%
var Count = 0;
for (var i = 0; i < Q.PageSize; i++) {
if (TheEnd)
{
Response.Write("<div class='hline' align='center'>");
Response.Write("<tr height='100' align='center'><td colspan='6'>Sorry, there are no Major Chains matching your request</td></tr><tr height='150'><td> </td ></tr>");
break;
}
if (Q.EOF) {
break;
}
r1 = Q("AdHead");
r2 = stripHTML(r1);
Response.Write("<td>"+r2+" </td>");
Response.Write("<td width='120'> </td></t r>");
Response.Write("<tr>");
Response.Write("</tr>");
if (Count > 1)
Count = 0;
else
Count++;
Q.MoveNext;
}
%>
</table>
<!--Next/Prev Page-->
<table width="765" cellspacing="0" cellpadding="0" border="0" class="smtxt">
<tr>
<td width="600" align="right"> </td>
<td width="130" align="right" class="flat">
<%
if (PageNo > 1) {
Response.Write("<a STYLE='text-decoration:non e;' href='?ID="+Request("ID")+ "&PageNo=" +PageNo+"& Move=Previ ous'>..Pre vious Page </a><br>");
}
%>
</td>
<td width="4"> </td>
<%
var i=0;
while((i<Q.PageCount)&&(i< 5))
{
Response.Write("<td><a STYLE='text-decoration:non e;' href='?ID=" + Request("ID") + "&PageNo=" + (i)+ "&Move=Next'>" + (i+1) + "  </a></td>");
i+=1;
}
%>
<td width="4"> </td>
<%
if (PageNo < Q.PageCount) {
Response.Write("<td width='110' align='right' class='flat'><a STYLE='text-decoration:non e;' href='?ID="+Request("ID")+ "&PageNo=" +PageNo+"& Move=Next' >Next Page.. </a></td>");
}
%>
</tr>
</table>
</body>
</html>
<%
Q.close();
Conn.close();
Conn = null;
%>
Here is my entire page of code can you show me where I should call the function.
Cheers
<%@ Language = "JavaScript" %>
<html>
<head>
<!-- #include file="../include/ado.inc" -->
<!-- #include file="../include/filters.a
<meta http-equiv="Content-Langua
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>StoreConnect Search</title>
<link href="ExecutionStyles.css"
<style type="text/css">
<!--
.flat {BORDER-RIGHT: #003399 1px solid; BORDER-TOP: #003399 1px solid; BORDER-LEFT: #003399 1px solid; BORDER-BOTTOM: #003399 1px solid; text-decoration: none}
.flatbutton {BORDER-RIGHT: #666666 1px solid; BORDER-TOP: #666666 1px solid; BORDER-LEFT: #666666 1px solid; BORDER-BOTTOM: #666666 1px solid}
.topl {BORDER-TOP: gray 1px solid}
.botl {BORDER-BOTTOM: gray 1px solid;font-family: Arial, Helvetica, sans-serif; font-size: 12px; color: #000000; text-align: left;}
.botsl {BORDER-BOTTOM: silver 1px solid;font-family: Arial, Helvetica, sans-serif; font-size: 12px; color: #000000; text-align: left;}
.norm {font-family: Verdana,Arial, Helvetica, sans-serif; font-size: 15px; color: #000000; text-align: left;}
.smtxt {font-family: Verdana,Arial, Helvetica, sans-serif; font-size: 13px; line-height: 15px; color: #000000; text-align: left;}
.vsmtxt {font-family: Verdana,Arial, Helvetica, sans-serif; font-size: 10px; line-height: 15px; color: #000000; text-align: left; text-decoration: none}
.hline {font-family: Verdana,Arial, Helvetica, sans-serif; font-size: 12px; color: #000000; text-align: left;}
//-->
</style>
<script language="JavaScript" type="text/javascript">
<!--
function stripHTML( oldString )
{
var newString = "";
var inTag = false;
for( var i = 0; i < oldString.length; i++ )
{
if( oldString.charAt(i) == '<' ) inTag = true;
if( oldString.charAt(i) == '>' ) {
inTag = false;
i++;
}
if( !inTag ) newString += oldString.charAt(i);
}
return newString;
}
//-->
</script>
</head>
<body TOPMARGIN="0" LEFTMARGIN="0" MARGINWIDTH="0" MARGINHEIGHT="0"">
<%
var Move;
var PageNo;
var TheEnd;
Move = Request("Move");
PageNo = Request("PageNo");
if (!(PageNo > 0 || PageNo < 999)) {
PageNo = 1;
TheEnd = 0;
}
var Q;
Q = Server.CreateObject("ADODB
strSQL = "select Retailer, Brand, ConnectCode, Min(RetailerLogo) as RTlogo, Min(AdHeader) as AdHead, Min(AdImage) as AdImg, Min(MajorChainLogo) as MCLogo, Min(MCName) as MCN from ADRetailersV "
strSQL += " where mcid = '"+Request("ID")+"'";
strSQL += " group by Retailer, Brand, ConnectCode";
strSQL += " order by Retailer";
Q.Open(strSQL, Conn, adOpenStatic);
Q.PageSize = 2;
if (!Q.EOF) {
if (Move == "Previous") {
if (PageNo > 1) {
PageNo--;
} else {
PageNo = 1;
}
}
else if (Move == "Next") {
if (Q.AbsolutePage < Q.PageCount) {
PageNo++;
} else {
PageNo = Q.PageCount;
}
}
else {
PageNo = 1;
}
Q.AbsolutePage = PageNo;
} else
{
TheEnd = 1;
}
%>
<!--Promos table-->
<table width="770" cellspacing=0 cellpadding=0 border=1 class="norm" valign="center">
<%
var Count = 0;
for (var i = 0; i < Q.PageSize; i++) {
if (TheEnd)
{
Response.Write("<div class='hline' align='center'>");
Response.Write("<tr height='100' align='center'><td colspan='6'>Sorry, there are no Major Chains matching your request</td></tr><tr height='150'><td> </td
break;
}
if (Q.EOF) {
break;
}
r1 = Q("AdHead");
r2 = stripHTML(r1);
Response.Write("<td>"+r2+"
Response.Write("<td width='120'> </td></t
Response.Write("<tr>");
Response.Write("</tr>");
if (Count > 1)
Count = 0;
else
Count++;
Q.MoveNext;
}
%>
</table>
<!--Next/Prev Page-->
<table width="765" cellspacing="0" cellpadding="0" border="0" class="smtxt">
<tr>
<td width="600" align="right"> </td>
<td width="130" align="right" class="flat">
<%
if (PageNo > 1) {
Response.Write("<a STYLE='text-decoration:non
}
%>
</td>
<td width="4"> </td>
<%
var i=0;
while((i<Q.PageCount)&&(i<
{
Response.Write("<td><a STYLE='text-decoration:non
i+=1;
}
%>
<td width="4"> </td>
<%
if (PageNo < Q.PageCount) {
Response.Write("<td width='110' align='right' class='flat'><a STYLE='text-decoration:non
}
%>
</tr>
</table>
</body>
</html>
<%
Q.close();
Conn.close();
Conn = null;
%>
Put the function with your <% tags I suppose just after the body will do.
ASKER
Not too sure what you mean here.
I've tried putting my function after <body> but its a javascript function so how does that work.
I've tried moving it around but seem to be getting nowhere.
:(
Thanks
I've tried putting my function after <body> but its a javascript function so how does that work.
I've tried moving it around but seem to be getting nowhere.
:(
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:
Accept: woodpd {http:#8305056}
Please leave any comments here within the next four days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
jAy
EE Cleanup Volunteer
I will leave the following recommendation for this question in the Cleanup topic area:
Accept: woodpd {http:#8305056}
Please leave any comments here within the next four days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
jAy
EE Cleanup Volunteer
function stripHTML( oldString )
{
var newString = "";
var inTag = false;
for( var i = 0; i < oldString.length; i++ )
{
if( oldString[i] == '<' ) inTag = true;
if( oldString[i] == '>' ) inTag = false;
if( !inTag ) newString += oldString[i];
}
return newString;
}