Click to See Complete Forum and Search --> : closing unclosed tags
PunkSktBrdr01
09-01-2003, 09:02 PM
Hello. I am making a fan fiction site, and when people write stories, the are able to include some formatting, such as italics, bold, align to center, etc... Anyway, if the author forgets to add the closing tag, it messes up the rest of the page. I was wondering if anyone could help me make a script to find unclosed tags and add the closing tags. Here's a link to one example of this problem:
http://www.radioactiverabbit.com/writing/viewstory.php?id=19
Also, I have another thread going in the CSS section concerning the problem of the overflowing text:
http://forums.webdeveloper.com/showthread.php?s=&threadid=16449
Thanks!
Can we see some example code of what you are using to make the text bold, etc... Just regular HTML tags?
PunkSktBrdr01
09-01-2003, 09:56 PM
Italics:
Bold:
Center:
I am using a function to parse through the story, which is extracted from a database. It replaces [i] with <i>, [b] with <b>, [c] with <div class="center"> (I have classes for all alignments in my CSS).
You should be able to get it from this exerpt from some of my code, which is what I use for the same thing (note that I got a bit of help from Jeff Mott with the regexp):
#bold
$message = preg_replace("/\[b\](.*?)\[\/b\]/","<b>\\1</b>",$message);Note, hit quote on my message to see my code... The forums mess it up...
PunkSktBrdr01
09-02-2003, 05:58 AM
I don't think that's what I'm looking for. I already have a function to replace the formatting with HTML. I need a way to find unclosed formatting ( without , etc...) and close it. Thanks!
That code will only turn text bold, if it finds the closing [/b] tag...
AdamGundry
09-02-2003, 07:43 AM
This will close any tags still open at the end of the string - including tags such as <br>. The nesting may not be perfect, but it seems to be more or less correct.
<?
function closeUnclosedTags($unclosedString){
// created by Adam Gundry, http://www.agbs.co.uk
preg_match_all("/<([^\/]\w*)>/", $closedString = $unclosedString, $tags);
for ($i=count($tags[1])-1;$i>=0;$i--){
$tag = $tags[1][$i];
if (substr_count($closedString, "</$tag>") < substr_count($closedString, "<$tag>")) $closedString .= "</$tag>";
}
return $closedString;
}
$str = '<p>Some <strong>unclosed <i>HTML</i>';
echo closeUnclosedTags($str);
?>
Adam
P.S. There might be a more efficient way of doing this - pyro, any ideas?
PunkSktBrdr01
09-02-2003, 03:19 PM
Thanks guys. I'll try them both.