Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Question about storing <span> tags in MySQL through PHP
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 &lt;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!

Report this thread to moderator Post Follow-up to this message
Old Post
clumsy_ninja
04-03-08 12:20 AM


Re: Question about storing <span> tags in MySQL through PHP
..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 &lt;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

Report this thread to moderator Post Follow-up to this message
Old Post
Michael Fesser
04-03-08 12:20 AM


Re: Question about storing <span> tags in MySQL through PHP
On 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


Report this thread to moderator Post Follow-up to this message
Old Post
NC
04-03-08 12:20 AM


Re: Question about storing <span> tags in MySQL through PHP
Thanks for the advice, all. :)

Report this thread to moderator Post Follow-up to this message
Old Post
clumsy_ninja
04-03-08 12:20 AM


Re: Question about storing <span> tags in MySQL through PHP
On 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().

Report this thread to moderator Post Follow-up to this message
Old Post
clumsy_ninja
04-03-08 12:20 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PHP Programming archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 06:24 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.