Dino
Dino

Reputation: 806

How to use str_replace with a condition

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>

Upvotes: 2

Views: 1988

Answers (2)

Dino
Dino

Reputation: 806

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.

Upvotes: 1

Calimero
Calimero

Reputation: 4298

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.

Upvotes: 5

Related Questions