Question about storing <span> tags in MySQL through PHP
Question about storing <span> tags in MySQL through PHP
am 02.04.2008 18:34:19 von clumsy_ninja
I have three CSS classes for elements: span.x, span.y, and
span.z. I'd like to store blocks of text in MySQL that utilizes the
three classes. In an attempt to save space and avoid
htmlentities() interpreting as <span$gt;, my solution was to
store 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 blah blah blah
span>
$str = str_replace('[x]', '', $str);
$str = str_replace('[/x]', '', $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 elements in a database? TIA!
Re: Question about storing <span> tags in MySQL through PHP
am 02.04.2008 18:49:06 von Michael Fesser
..oO(clumsy_ninja)
>I have three CSS classes for elements: span.x, span.y, and
>span.z. I'd like to store blocks of text in MySQL that utilizes the
>three classes. In an attempt to save space and avoid
>htmlentities() interpreting as <span$gt;, my solution was to
>store 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 blah blah blah
>span>
> $str = str_replace('[x]', '', $str);
> $str = str_replace('[/x]', '', $str);
> // [...] repreat process for [y] and [z]
> return $str;
You could try a regular expression:
$pattern = '#\[(.+)](.+)\[/\1]#U';
$replace = '\2';
$str = preg_replace($pattern, $replace, $str);
Micha
Re: Question about storing <span> tags in MySQL through PHP
am 02.04.2008 20:01:35 von nc
On Apr 2, 9:34 am, clumsy_ninja wrote:
>
> I have three CSS classes for elements: span.x, span.y,
> and span.z. I'd like to store blocks of text in MySQL that
> utilizes the three 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
Re: Question about storing <span> tags in MySQL through PHP
am 02.04.2008 22:27:25 von clumsy_ninja
Thanks for the advice, all. :)
Re: Question about storing <span> tags in MySQL through PHP
am 02.04.2008 22:41:42 von clumsy_ninja
On Apr 2, 3:27 pm, clumsy_ninja 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 A Tale of Two Cities.
My problem is preserving the < and > in html tags while converting
things like quotes w/ htmlentities().
Re: Question about storing <span> tags in MySQL through PHP
am 04.04.2008 12:08:31 von Michael Fesser
..oO(clumsy_ninja)
>On Apr 2, 3:27 pm, clumsy_ninja 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 A Tale of Two Cities.
>
>My problem is preserving the < and > in html tags while converting
>things like quotes w/ htmlentities().
OK, that was my fault. Of course replacing [foo]...[/foo] first and
calling htmlentities() wouldn't work in this order. Ignore the first
part of my reply. ;)
Micha