2

I want to use str_replace with some conditions. I am developing an application which inputs a block of text from a text area and output it as 1 line. Whenever an "end of line" or "space + with end of line" is met, the string is replaced by <br>

I have now came with the solution that whenever and end of line is met, the string is replaced by <br>. But if the user types a space before end of line , i need to get rid of that space too before replacing the EOL with <br>.

MY CODE

$refresheddata = str_replace("\n", '<br>', $data);

SAMPLE INPUT

This is the first line with a space at the end 
This is the second line which donot have a space at the end

OUTPUT OF MY CODE

This is the first line with a space at the end <br>This is the second line which donot have a space at the end

REQUIRED OUTPUT

This is the first line with a space at the end<br>This is the second line which donot have a space at the end

Check the space before <br>

FULL CODE

<?php 
$page = $data = $title =  $refresheddata = '';
if($_POST){
    $page = $_POST['page'];
    $data = $_POST['data'];
    $title = $_POST['title'];
    $refresheddata = str_replace("\n", '<br>', $data);
    $refresheddata = htmlentities($refresheddata);
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Data</title>
</head>
<body>
<form method="post">
    <h3>Original Text</h3>
    <input type="text" name="title" placeholder="Enter your title here.." style="font-size:16px; padding:10px;" required><br><br>
    <input type="text" name="page" placeholder="Enter your page data here.." style="font-size:16px; padding:10px;" required><br><br>
    <textarea name="data" rows="15" style="width:100%" placeholder="Enter your remaining contents here..." required></textarea>
    <input type="submit">
</form><br><br>

<h3>Result Text</h3>
&lt;START&gt;<br>
&lt;TITLE&gt;<?php echo $title; ?>&lt;/TITLE&gt;<br>
&lt;BODY&gt;<br>
&lt;P&gt;<?php echo $page; ?>&lt;/P&gt;<br>
&lt;P&gt;<?php echo $refresheddata; ?>&lt;/P&gt;<br>
&lt;/BODY&gt;<br>
&lt;END&gt;
</body>
</html>
Dino
  • 806
  • 1
  • 8
  • 22

2 Answers2

5

The easy way, just replace both :

$refresheddata = str_replace([" \n","\n"], '<br>', $data);

It can also be done with a simple regexp, like this one

$refresheddata = preg_replace("/ ?\n/",'<br>',$data);

The regexp solution might be more versatile as it can also be updated to handle slightly different other cases as well, such as more than one space preceding a newline at the same time. Choose acording to your needs and how you will be better able to maintain the code.

Calimero
  • 4,238
  • 1
  • 23
  • 34
  • 1
    Note that the order is important if you use `str_replace` otherwise the spaces will not go away – apokryfos Oct 07 '17 at 10:09
  • Issue is not resolved. I have updated the question with full code. – Dino Oct 07 '17 at 11:28
  • See this demo : https://3v4l.org/ciXIs the solution does work acording to the initial context you provided. The data going through your form changes the game somewhat (newlines might not just be simple \n chars, etc) – Calimero Oct 07 '17 at 11:56
  • see this question (and answers) for a lead to your new problem : https://stackoverflow.com/questions/7836632/how-to-replace-different-newline-styles-in-php-the-smartest-way – Calimero Oct 07 '17 at 12:01
  • 1
    @Calimero Thanks for the answer. Since i'm taking my input from a text box, you solution doesn't work. I have searched and came up with the answer. – Dino Oct 09 '17 at 16:54
1

Finally I got the answer. Since Im using textarea for the input, all other answers are wrong, I still get the space at the EOL. I tried replacing my str_replace functions parameters and got the desired output.

SOLUTION

$refresheddata = str_replace(array(" \r\n","\r\n"), '<br>', $data);

Now the new line is gone with the space before it.

Dino
  • 806
  • 1
  • 8
  • 22