str_replace(), and correctly positioned HTML tags

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



PHP list,

This may be a simple matter. Please feel free to tell me to RTFM if you can direct me to exactly where in the FM to R. Or, alternately, please use simple explanations, as I'm not an experienced PHP coder.

I'm building a simple content management system, where users can enter text into a web form. The text is stored in a MySQL database. The text should be plain text.

When the text is retrieved from the database for display, I want to add some HTML tags so I can control the format with an external CSS.

I'm assuming the best way to do this is with str_replace(). But there are some complications which make me unsure of its usage.

First, here is the code I initially made:
$content = "<p>" . str_replace("\r\n", "</p>\n<p>", $text) . "</p>\n";
echo $content;

The problem is that I want to give the users the ability to add text that will be converted to <h3> tags. I figure the best and easiest way to do this is give them some text markers, like "--++" and "++--" that can be converted to <h3> and </h3> respectively.

So I guess I do:
$content = str_replace("--++", "<h3>", $text);
$content1 = str_replace("++--", "</h3>", $content);
$content2 = "<p>" . str_replace("\r\n", "</p>\n<p>", $content1) . "</p>\n";
echo $content2;

But of course a user is likely to put their <h3> heading at the beginning of their text. Which would generate:
<p><h3>Heading</h3>text text text</p>

That's not good. What I need is:
<h3>Heading</h3><p>text text text</p>

And figuring out how to do that was where my brain stopped.

I need to be able to account for circumstances where it may not be appropriate to arbitrarily put a <p> tag at the beginning.

As well as <h3> tags, there may also be things such as images and <hr> lines, but they are all similar in that they will take some text code and convert into an HTML entity.

I need to be able to separate those out and then be able to place opening and closing <p> tags at the right place before and after paragraphs.

Is there a way to do this? Is there a good tutorial available?

Any advice appreciated. Thank you for taking the time to read this.

--
Dave M G

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux