Reply to A (hopefully) simple regular expression question...

Your name:

Reply:


Posted by alexrussell101 on 01/12/06 21:09

For anyone who can't be bothered to read my code and examples, scroll
to the bottom, the question's there. Thanks.

I'm using php and regular expressions to convert bbcode style things to
html. My code to convert something like this:

[quote Bob]
Hello there
[/quote]

to something like this:

<fieldset>
<legend>&nbsp;Bob&nbsp;</legend>
<div class="quote">Hello there</div>
</fieldset>

goes something like this:

$ret = preg_replace("/\[quote (.+?)\](.+?)\[\/quote\]/i", "\n
<fieldset>\n <legend>&nbsp;\\1&nbsp;</legend>\n <div
class=\"quote\">\\2</div>\n </fieldset>\n ", $ret);


Now I have some code that cuts the text at a certain point (in order to
get a general preview of a message) using this function:

function cut_post($string, $length = DEFAULT_CUT_LENGTH) {
if (strlen($string) > $length) { // if the string IS too long...
$ret = substr($string, 0, $length); // cut it

for ($i = strlen($ret) - 1; $i >= 0; $i--) { // going from the
end to the beginning
if (substr($ret, $i, 1) == "<") { // if the current
character is an html open tag (<)
if (substr($ret, $i + 1, 1) != "/") { // if we found
anything except "</" (i.e. an actual start tag)
$ret = substr($ret, 0, $i); // return everything up
to that point
$i = -1; // and set our pointer to before the
string starts so we don't carry on looking
} else {
$i = -1;
}
}
}

if (substr($ret, strlen($ret) - 1, 1) == " ")
$ret = substr($ret, 0, strlen($ret) - 1);

$ret .= "...";
} else { // if it's not too long...
$ret = $string; // don't cut it. amazing!
}

return $ret;
}


I have a problem when it just happens to cut half way through a long
quote the post cutter does its job and cuts everything from the last
HTML tag it may have been half way through (the <div> inside the
<fieldset>) but unfortunately doesn't account for the open <fieldset>
and it never gets closed as the post was cut there, ending up with HTML
similar to:

<fieldset>
<legend>&nbsp;Bob&nbsp;</legend>
<div class="quote">...

and then the page continues, messing up my nice formatting. So my
question is:

Is there a way to use a regular expression to search for an open
<fieldset> and close it?
I'm pretty sure there is, but I'm not so good at those negative
regexps, I get that you'd have to search the output HTML for <fieldset>
followed by any string of characters and NOT </fieldset> and then if
that matches, then add </fieldset> to the end. Although that also
doesn't close the </div> although I'm not sure how it manages not to
close that (well actually cut it out) itself.

Hmm maybe I don't want to just close stuff at all... it might just be
best to remove the quote altogether or something. Anyway, can anyone
help? It's not a major problem, but it ruins my layout, and I like my
layout.

Well thanks to anyone who can help, and sorry I've rambled on. Just
trying to give as much info as I can.

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация