I have got this php code
$Text=htmlspecialchars($_POST['new_post'],ENT_QUOTES);
$Text=trim($Text);
Text
is variable that is defined by user.If text
is
There is a pleasure in the pathless woods,
There is a rapture on the lonely shore,
There is society, where none intrudes.
I want it to stay like that.But if Text
is
By the deep Sea, and music in its roar:
I love not Man the less, but Nature more,
From these our interviews, in which I steal
I want it to be
By the deep Sea, and music in its roar:
I love not Man the less, but Nature more,
From these our interviews, in which I steal
How can i achieve that with PHP?
A simple solution would be to use preg_replace()
. This will find any multiple lines, and replace them with a single new line:
$Text = preg_replace("/[
]+/", "
", $Text);
See a demo here:
$Text = "There is a pleasure in the pathless woods,
There is a rapture on the lonely shore,
There is society, where none intrudes.";
$Text=trim($Text);
$Text=preg_replace("/[
]+/", "
", $Text);
echo $Text;
You could use str_replace for this
str_replace(array("
", ""), '', $text);
Also any reason you're escaping on input rather than output?