store whitespace but not interpret it in <br>

A

Anonymous

Guest
Hello all,

my problem is the following: I have a page where the user enters some data, let's say :

string1 = 'AGREHREHRHREHRE';
string2 = 'WEFEWGEWGEGEWGEGEGWG';

These data I use in another page, so I use the "<pre>" tag to display what the user has entered. The problem arrives when the user writes something like EWGEGEW EFGEGEW EGRGREGRE RGVFVDSVDS

This string, when I pass it into a variable, it is stored with newlines. I don't want to make any substitution of the newlines, as I want to preserve the spaces in the next page, and display the user's sequence as entered and not with 3 <br>'s, because there were 3 spaces in the original string...
any ideas?
 
<pre> means "preserver white-space". If the text contains newlines, they are shown as newlines. They are not really substituted by <br />'s but it looks like that.

What you could do is this:

Code:
<?php
$stringFromUser = "EWGEGEW\nEFGEGEW\n\n\nEGRGREGRE\n\nRGVFVDSVDS\n";
// or ofcourse coming from the form

$stringToPrint = preg_replace("!\n+!", "\n", $stringFromUser);
echo '<pre>' . $stringToPrint . '</pre>';
?>

Coditor
 
Back
Top