RE: [PHP] str_replace() function help

From: Date: Tue, 28 Aug 2001 17:58:35 +0000
Subject: RE: [PHP] str_replace() function help
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-64852@lists.php.net to get a copy of this message
But that's not the problem. I know str_replace can handle what you just wrote. My problem is replacing TWO <br /> tags at the same time. Take a look at my script and you'll know what I mean. Can anyone help me with this? -----Original Message----- From: J Friesen [mailto:jeff5300@mb.sympatico.ca] Sent: Tuesday, August 28, 2001 11:07 AM To: Navid Yar Cc: Php-General@Lists. Php. Net Subject: RE: [PHP] str_replace() function help 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:Superbus22@home.com] 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: php-general-unsubscribe@lists.php.net For additional commands, e-mail: php-general-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net

« previous php.general (#64852) next »