Help - Search - Members - Calendar
Full Version: Replace strings with PHP
Invision Power Services > Community Forums > Community Web Design and Coding
Sebastian Mares
Hi there!

I am having some problems replacing strings using PHP. I have a MySQL table from where I fetch data. For new lines, I use \n in the MySQL table and when outputting this data, I would like to convert \n to <br />. This is what I use:

CODE
function FormatStuff($stuff) {
         $stuff = strtr($stuff, "\n", "<br />");
         return $stuff;
       }


The function is called using:

CODE
FormatStuff($row[4])


The above doesn't seem to work. The data which the function throws out is the same as the data passed. Do I have to escape some characters or someting?

Regards,
Sebastian
Sebastian Mares
Nevermind... I found a better method:

CODE
function FormatStuff($stuff) {
         $raw = array('\n', 'ö', 'ä', 'ü', 'Ö', 'Ä', 'Ü', 'ß',);
         $formatted = array('<br />', '&ouml;', '&auml;', '&uuml;', '&Ouml;', '&Auml;', '&Uuml;', '&szlig;',);
         for($x=0;$x<sizeof($raw);$x++) {
           $stuff = str_replace($raw[$x],$formatted[$x],$stuff);
         }
         return $stuff;
       }
Michael Boutros
There is a function called nl2br() which changes newlines into <b>.

The best thing to do is format $stuff before it goes into the database. For example, you could do the following:

$stuff2 = nl2br($stuff);

Then in your INSERT query, replace $stuff with $stuff2.

http://ca.php.net/nl2br

Hope this helps original.gif
Michael Boutros
Also, if you want to convert thigns like < into the letter formation (I have it in my head but I just cant grasp it) you could use htmlspecialchars, which does that for you.

http://ca.php.net/htmlspecialchars
bılʞ
QUOTE(Sebastian Mares @ Mar 6 2005, 09:38 PM)
Nevermind... I found a better method:

CODE
function FormatStuff($stuff) {
         $raw = array('\n', 'ö', 'ä', 'ü', 'Ö', 'Ä', 'Ü', 'ß',);
         $formatted = array('<br />', '&ouml;', '&auml;', '&uuml;', '&Ouml;', '&Auml;', '&Uuml;', '&szlig;',);
         for($x=0;$x<sizeof($raw);$x++) {
           $stuff = str_replace($raw[$x],$formatted[$x],$stuff);
         }
         return $stuff;
       }

*


That is by no means a better method. tongue.gif

Try this:

CODE
function format_string( $unformatted )
{
  $formatted = htmlentities( $unformatted );
  $formatted = nl2br( $formatted );

  return $formatted;
}
Sebastian Mares
QUOTE(Den Watts @ Mar 7 2005, 02:36 AM)
That is by no means a better method.  tongue.gif

Try this:

CODE
function format_string( $unformatted )
{
  $formatted = htmlentities( $unformatted );
  $formatted = nl2br( $formatted );

  return $formatted;
}

*


htmlentities works, but not the new line conversion. As I said, the MySQL table has strings like "Hello\nWorld". When fetching the data, I want "\n" to be converted to "<br />" before it gets displayed in the user's browser.
flightmike1
Thats what nl2br is supposed to do. If its not workign you could always just go str_replace("\n", '<br />', $str);
Sebastian Mares
QUOTE(flightmike1 @ Mar 7 2005, 10:52 AM)
Thats what nl2br is supposed to do. If its not workign you could always just go str_replace("\n", '<br />', $str);
*


Done that, too, but for some strange reason, it doesn't work. blink.gif
Rikki
Hold on, the database actually displays the \n? Why? blink.gif It shouldn't do, your content should just go on the next line. \n is one of the 'hidden' characters.

Anyway, if the \n is actually shown in the database and you don't want to change it then use:

str_replace('\n', "<br />", $str);

The single quotes around the \n are important.
Sebastian Mares
QUOTE(Rikki @ Mar 7 2005, 12:37 PM)
Hold on, the database actually displays the \n? Why? blink.gif It shouldn't do, your content should just go on the next line. \n is one of the 'hidden' characters.

Anyway, if the \n is actually shown in the database and you don't want to change it then use:

str_replace('\n', "<br />", $str);

The single quotes around the \n are important.
*


Placing single quotes around both \n and <br /> helped. Thanks Rikki.
About the \n in the table... I entered the data using the SQL Query Browser and didn't find a way how to insert a new line, so I wrote everything as one line and used \n in the place where I wanted a new line to begin. tongue.gif
Rikki
OK well that isn't a good way of doing it. If you want to do it like that then you might as well just put the <br /> in the database.
Stephen
yes by entering \n by hand PHP "sees" the character \ and the character n instead of the character \n (\n is a single character, as there is no human readable form (the first 31 ASCII characters are called 'control characters' and none of them are human readable characters) for it that is how PHP displays it)

http://www.lookuptables.com/
Sebastian Mares
OK, I changed the "\n" to a real \n and nl2br() works. One question, though... Is it possible to suppress the new line character and only substitute it by "<br />"? The reason is that I am using indentation in my HTML and when a table entry contains a new line, the code-flow is screwed:

CODE
...
<tr>
<td class="even" id="pupil_info" width="35%"><span class="fancy_letter">W</span>ohnort:</td>
<td class="even">49&deg; 2' 38&quot; n&ouml;rdliche Breite<br />
8&deg; 22' 52&quot; &ouml;stliche L&auml;nge<br />
(Karlsruhe, Neureut)</td>
</tr>
...


What I would like is:

CODE
...
<tr>
<td class="even" id="pupil_info" width="35%"><span class="fancy_letter">W</span>ohnort:</td>
<td class="even">49&deg; 2' 38&quot; n&ouml;rdliche Breite<br />8&deg; 22' 52&quot; &ouml;stliche L&auml;nge<br />(Karlsruhe, Neureut)</td>
</tr>
...


innocent.gif
Brendon Koz
QUOTE(Jervous @ Mar 6 2005, 02:39 PM)
There is a function called nl2br() which changes newlines into <b>.

The best thing to do is format $stuff before it goes into the database. For example, you could do the following:

$stuff2 = nl2br($stuff);

Then in your INSERT query, replace $stuff with $stuff2.

http://ca.php.net/nl2br

Hope this helps original.gif
*



Actually, that's a bad practice. Through testing and debugging, I found out that nl2br() does not remove/convert the newline character, it only places an HTML newline AFTER the C-style newline character. What's this mean? If you store that in the database, you've got a LOT of extra junk in there that's not needed. Store it in the database as a newline, and use str_replace().

Sebastian, this should help solve your flow problem too. Except...since you've already implemented it, you'll have to make sure to query your database to remove all the <br>'s.
flightmike1
QUOTE(malikyte @ Mar 9 2005, 02:48 PM)
Actually, that's a bad practice.  Through testing and debugging, I found out that nl2br() does not remove/convert the newline character, it only places an HTML newline AFTER the C-style newline character.  What's this mean?  If you store that in the database, you've got a LOT of extra junk in there that's not needed.  Store it in the database as a newline, and use str_replace().

Sebastian, this should help solve your flow problem too.  Except...since you've already implemented it, you'll have to make sure to query your database to remove all the <br>'s.
*

I prefer to have a real line-break after an HTML one because it is a lot cleaner and results in nicer markup.
bılʞ
QUOTE(malikyte @ Mar 9 2005, 01:48 AM)
Actually, that's a bad practice.  Through testing and debugging, I found out that nl2br() does not remove/convert the newline character, it only places an HTML newline AFTER the C-style newline character.  What's this mean?  If you store that in the database, you've got a LOT of extra junk in there that's not needed.  Store it in the database as a newline, and use str_replace().

Sebastian, this should help solve your flow problem too.  Except...since you've already implemented it, you'll have to make sure to query your database to remove all the <br>'s.
*


If you had've visited the PHP.net documentation on nl2br(), you would have found this out WITHOUT having to do "testing and debugging", the <br /> gets added before the new line too. original.gif

http://uk.php.net/nl2br
Brendon Koz
At the time, I was learning from a book that was purchased for a class. I've since gone way beyond that level. tongue.gif

flightmike1, you'd still be better off saving it as a standard newline in the database, and then using nl2br() on the retrieved content. wink.gif People that use TABs or spaces to indent their HTML code don't like the newline because it breaks their flow. But I do understand where you're coming from.
Ice_Blade
If you really want tabs in your html you could also use \t which is the tab character.
Alot of servers also see both \r and \n type newlines, be careful which you use, and on what kind of a server you're working wink.gif
It can be a real pain. innocent.gif
_
You could just use CR, LF or CRLF - depending on the OS - in the database...
Brendon Koz
/r = carriage return
/n = newline

.../n will also sometimes be both newline+carriage return, depending on the OS
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.