Code Comments
Programming Forum and web based access to our favorite programming groups.I have three CSS classes for <span> elements: span.x, span.y, and
span.z. I'd like to store blocks of text in MySQL that utilizes the
three <span> classes. In an attempt to save space and avoid
htmlentities() interpreting <span> as <span$gt;, my solution was to
store <span> tags as [x]blah blah blah[/x] in the database and create
a function that is called when the block of text needs to be displayed
in a browser:
function interpret($str) {
$str = htmlentities($str);
// replace [x]blah blah blah[/x] with <span class="x">blah blah blah</
span>
$str = str_replace('[x]', '<span class="x">', $str);
$str = str_replace('[/x]', '</span>', $str);
// [...] repreat process for [y] and [z]
return $str;
}
This seems like an extremely inefficient way to go about doing things,
especially for large blocks of text. Any suggestions on a better way
to go about preserving the use of <span> elements in a database? TIA!
Post Follow-up to this message..oO(clumsy_ninja)
>I have three CSS classes for <span> elements: span.x, span.y, and
>span.z. I'd like to store blocks of text in MySQL that utilizes the
>three <span> classes. In an attempt to save space and avoid
>htmlentities() interpreting <span> as <span$gt;, my solution was to
>store <span> tags as [x]blah blah blah[/x] in the database and create
>a function that is called when the block of text needs to be displayed
>in a browser:
>
>function interpret($str) {
> $str = htmlentities($str);
Use htmlentities() (or better htmlspecialchars(), which should be enough
in most cases) only before you output the final string to the HTML page.
If you want to manipulate the data first, do it on the raw string.
> // replace [x]blah blah blah[/x] with <span class="x">blah blah blah</
>span>
> $str = str_replace('[x]', '<span class="x">', $str);
> $str = str_replace('[/x]', '</span>', $str);
> // [...] repreat process for [y] and [z]
> return $str;
You could try a regular expression:
$pattern = '#\[(.+)](.+)\[/\1]#U';
$replace = '<span class="\1">\2</span>';
$str = preg_replace($pattern, $replace, $str);
Micha
Post Follow-up to this messageOn Apr 2, 9:34 am, clumsy_ninja <lastsli...@yahoo.com> wrote: > > I have three CSS classes for <span> elements: span.x, span.y, > and span.z. I'd like to store blocks of text in MySQL that > utilizes the three <span> classes. So store them as-is. There is absolutely no reason to run htmlentities() on a piece of HTML before storing it in a database. Just make sure you prevent SQL injection... Cheers, NC
Post Follow-up to this messageThanks for the advice, all. :)
Post Follow-up to this messageOn Apr 2, 3:27 pm, clumsy_ninja <lastsli...@yahoo.com> wrote: > Thanks for the advice, all. :) Oh, actually, a bit of clarification about my question... I'm not trying to call htmlentites() when I'm storing the text, only when retreving the text to display in a webpage. For example, the following would be stored in the database: "It was the best of times, it was the worst times," he quoted from [book]A Tale of Two Cities[/book]. Which would be "translated" to this markup for a webpage: "It was the best of times, it was the worst times," he quoted from <span class="book">A Tale of Two Cities</span>. My problem is preserving the < and > in html tags while converting things like quotes w/ htmlentities().
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.