Dave M G wrote:
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

It seems like your life would be a lot easier with break takes instead of paragraph tags. Break tags behave like newlines, so work rather well for a 1 to 1 correspondance when turning \r\n into <br />. This way, you don't have to worry about the opening tags.

The fastest way to do this may be the function nl2br(), which takes only one parameter, your string, and returns your string with the newlines replaced by break tags.

Another piece of advice is that you are creating a lot of strings. Do you need the string $text to be unmodified? Why don't you do this:

$text = str_replace("--++", "<h3>", $text);
$text = str_replace("++--", "</h3>", $text);
$text = "<p>" . str_replace("\r\n", "</p>\n<p>", $text) . "</p>\n";
echo $text;

Remember that PHP works by value, not by reference. So what happens in that first line is that it makes a copy of $text, does the replacement on that copy, and then overwrites $text with that copy. With your method you are creating a bunch of strings that I would imagine go unused later in your script.

Since you seem to want to maintain a newline in the HTML source, nl2br might not be exactly perfect for you. Here is your original code modified to work with break tags:

$text = str_replace("--++", "<h3>", $text);
$text = str_replace("++--", "</h3>", $text);
$text = str_replace("\r\n", "<br />\n", $text);
echo $text;

And here is the same code using arrays and one single replace instruction to do it, since replacement order doesn't matter:

$text = str_replace(array("--++", "++--", "\r\n"), array("<h3>", "</h3>", "<br />\n"), $text);
echo $text;

And of course, if you never do anything with your formatted string except echoing it out, don't store it, just echo it:

echo str_replace(array("--++", "++--", "\r\n"), array("<h3>", "</h3>", "<br />\n"), $text);

So, to sum up my advice:

1) Don't create extra variables that you will never use
2) Consider using break tags instead of paragraph tags, they're easier to deal with in your situation.
3) Use arrays for replacement when appropriate
4) Don't store data if you're only ever going to echo it out right away and never use it again.

I think that's it, unless I missed something.

Regards, Adam Zey.

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

Reply via email to