View Full Version : Check for close/end tag
Morgoth
06-05-2005, 03:21 AM
I wrote a script that takes user input and posts it in an html document. The script allows the user to type three (maybe more later) html tags for what ever reason they want.
Tags:
<b>Bold</b>
<i>Italics</i>
<u>Underline</i>
That's where I have a problem.
If a user types <b> without an end tag (</b>) it will make everything after the user's text bolded and I don't want that
What would be the best method to check and add the end tag if necessary?
Morgoth
06-17-2005, 04:13 AM
Well, I thought up a couple of ideas, and this one seemed to work the best, so here it is. I have tested this code a bunch of times with a bunch of different combinations. Please test it yourself, and if you find any problems please tell me. :cool:
<%Option Explicit%>
<%
Private Function TagCloser(ByVal strString)
Dim strTemp, strStart(2), strEnd(2), intCount, I, J
strStart(0) = "<b>"
strEnd(0) = "</b>"
strStart(1) = "<i>"
strEnd(1) = "</i>"
strStart(2) = "<u>"
strEnd(2) = "</u>"
For I = 0 To 2
strTemp = LCase(strString)
intCount = UBound(Split(strTemp, strStart(I)))
strTemp = Mid(strTemp, InStr(1, strTemp, strStart(I)) + 1)
intCount = intCount - UBound(Split(strTemp, strEnd(I)))
For J = 1 To intCount
strString = strString & strEnd(I)
Next
Next
TagCloser = strString
End Function
Dim strMyString
strMyString = "</b>Text <b>Bold</b> <i><i><i><b><u>Text<u></u><u> <b><b>Bold Text"
Response.Write TagCloser(strMyString)
Response.Write "<br>"
Response.Write "This text will <b>not</b> be effected!"
Response.Write "<br><br>"
Response.Write strMyString
Response.Write "<br>"
Response.Write "This text will be effected!"
%>
fractalvibes
06-17-2005, 06:50 AM
So how do you determine what extent of content the user wants to be bold/italic/etc.? If the user has the very high end technical skill to do something as complex as create a Word document with certain phrases having bold and italic attributes, then surely they can add an ending tag or live with the consequences....? ;-)
Sometimes I think we have to program for a tribe of people who have never seen fire....
fv
Morgoth
06-17-2005, 08:40 PM
So how do you determine what extent of content the user wants to be bold/italic/etc.? If the user has the very high end technical skill to do something as complex as create a Word document with certain phrases having bold and italic attributes, then surely they can add an ending tag or live with the consequences....? ;-)
Sometimes I think we have to program for a tribe of people who have never seen fire....
fv
This code just protects the pervious posts. It adds the end tags at the end of the current user's post to stop from leak over that will effect the rest of the text on the page.
So, if a user forgot to add the "</b>" after their first word (which is the only word they wanted to bold), the rest of the user's text and only the user's text will suffer.
If you have ever used phpbb, you will notice that it has some sort of feature like this for it's tags.
FractalVibes, have you tested the code? Do you like it?
fractalvibes
06-17-2005, 08:54 PM
Hadn't tried, but looks like would work.
fv
Morgoth
06-20-2005, 09:08 PM
I also wrote one in PHP:
<?php
$String = "</b>Text <b>Bold</b> <i><i><i><b><u>Text<u></u><u> <b><b>Bold Text";
$Start = Array("<b>", "<i>", "<u>");
$End = Array("</b>", "</i>", "</u>");
For ($i=0; $i<=2; $i++)
{
$Temp = StrToLower($String);
$Count = SubStr_Count($Temp, $Start[$i]);
$Temp = SubStr($Temp, StrPos($Temp, $Start[$i]));
$Count = $Count - SubStr_Count($Temp, $End[$i]);
For ($j=1; $j<=$Count; $j++)
{
$String = $String . $End[$i];
}
}
echo $String;
?>
Does anyone know any other scripts in anyother languages that do this?
jaywhy13
06-23-2005, 04:05 PM
Try utilizing regular expressions :D
Morgoth
06-23-2005, 05:56 PM
Try utilizing regular expressions :D
I am trying to find tutorials to learn and write the patterns I need for other projects that I can use regular expressions, but google isn't helping me.
I found one page that helps with my BBcode to html functions, but it's not helping me learn it very well.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.