This is the code I use for articles that are taken out of a database and the
\n need to be changed to paragraphs

$art = htmlspecialchars($art);
$art = nl2br($art);
$art = str_replace("<br />", "</p><p>", $art);
echo "<p>" . $art . "</p>";

-----Original Message-----
From: Navid Yar [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, August 28, 2001 4:08 AM
To: Php-General
Subject: [PHP] str_replace() function help


Hello,

I am creating an online text editor for a magazine site and I want to be
able to apply some CSS formatting into it. The following is the script that
adds the <br /> tags where necessary, then it strips the <br /> tags by
replacing them with some <p> tags with CSS formatting. The problem I'm
having is that it will not pick up on those <br /> tags (2 at a time). I
think it has something to do with the newline (\n) and return (\r) escape
characters not capturing the <br /> tags. Maybe I didn't set it up
correctly. Below the code is the input (exactly as typed in the online text
editor), the undesired output I'm getting when I run the article through the
script, and the desired output that I want.

Please Note that I am trying to comply with the XHTML standards and will be
converting these into XHTML, therefore all tags must be closed ( example:
<p></p> ), and all empty tags must be closed in this way: <tag /> ( example:
<br /> ). Also note that I am trying to stay away from other pattern
matching and replacing functions. The reason is that str_replace() seems
much faster than the rest. If it is possible, I would like to stick with
str_replace(). But if this turns out to be too complex for str_replace to
handle, then I will consider other options. I am on a Win2000 platform
running PHP 4.0.6 (which may or may not be relevant). Thanks in advance this
anyone who helps me out with this, I would really appreciate it.


THE SCRIPT
__________________

<html>
<head><title>str_replace</title></head>
<body>

<?php

if ($article) {
   $article = nl2br(htmlspecialchars(stripslashes($article)));
   $article = "<p class=\"arialnn16\">\n" . $article . "</p>\n\n";
   $article = str_replace("<br />\r\n<br />","</p>\n<p
class=\"arialnn16\">\n",$article);
   echo "$article";
} else {
echo "Please include an article before sending.";
}

?>

</body>
</html>



INPUT
_______

There are numerous definitions for the word hero. I would like to define
heroes as individuals who inspire us to emulate their level of perseverance
and success. Simply put, heroes are people we look up to.

Life is full of celebrated heroes: sports figures, individuals from all
walks of the entertainment industry, famous and infamous politicians, and
distinguished individuals from the business and science arenas, just to name
a few. But for every celebrated hero, there is at least one unsung hero.
This "Spotlight" segment is dedicated to the unsung heroes of our time.

Recently, I have been corresponding via email with three very dedicated and
talented students from the University of Toronto whom I consider real life
heroes. Once you read about their accomplishments as well as their trials
and tribulations in life and the positive example they intend to set for the
Afghan youths everywhere, I am sure you will agree with my assessment.



THE UNDESIRED OUTPUT
______________________

<html>
<head><title>str_replace</title></head>
<body>

<p class="arialnn16">
There are numerous definitions for the word hero. I would like to define
heroes as individuals who inspire us to emulate their level of perseverance
and success. Simply put, heroes are people we look up to.
<br />

<br />
Life is full of celebrated heroes: sports figures, individuals from all
walks of the entertainment industry, famous and infamous politicians, and
distinguished individuals from the business and science arenas, just to name
a few. But for every celebrated hero, there is at least one unsung hero.
This &quot;Spotlight&quot; segment is dedicated to the unsung heroes of our
time.
<br />

<br />
Recently, I have been corresponding via email with three very dedicated and
talented students from the University of Toronto whom I consider real life
heroes. Once you read about their accomplishments as well as their trials
and tribulations in life and the positive example they intend to set for the
Afghan youths everywhere, I am sure you will agree with my assessment.</p>

</body>
</html>



DESIRED OUTPUT
__________________

<html>
<head><title>str_replace</title></head>
<body>

<p class="arialnn16">
There are numerous definitions for the word hero. I would like to define
heroes as individuals who inspire us to emulate their level of perseverance
and success. Simply put, heroes are people we look up to.
</p>

<p class="arialnn16">
Life is full of celebrated heroes: sports figures, individuals from all
walks of the entertainment industry, famous and infamous politicians, and
distinguished individuals from the business and science arenas, just to name
a few. But for every celebrated hero, there is at least one unsung hero.
This &quot;Spotlight&quot; segment is dedicated to the unsung heroes of our
time.
</p>

<p class="arialnn16">
Recently, I have been corresponding via email with three very dedicated and
talented students from the University of Toronto whom I consider real life
heroes. Once you read about their accomplishments as well as their trials
and tribulations in life and the positive example they intend to set for the
Afghan youths everywhere, I am sure you will agree with my assessment.</p>

</body>
</html>


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to