My problem is pretty simple. I want to change new lines in text area to <br>
tags BUT I need the final string to be one-line text. I tried using nl2br
function but as a result I get string with <br>
tags and new lines. I also tried to simply replace 
 or 
 symbols with <br>
using str_replace
but it doesn't work.
Here is sample of my latest code:
Godziny otwarcia: <textarea name="open" rows="3" cols="20">'."$openh".'</textarea>
<input type="submit" name="openb" value="Zmień"/><br>
if($_POST['openb']) {
$open = $_POST['open'];
str_replace('
', '<br>', $open);
change_data(21, $open);
}
The $openh
is result of this:
$tab = explode('<br>', $openh);
$openh = null;
for($i=0;$i<count($tab);$i++)
$openh = $openh . $tab[$i] . '
';
(yes, I know i could use str_replace
, don't ask why I did it this way)
and the original $openh
is $openh = 'Pon-pt 9:00-17:00<br>Środa 12:00-17:00'
Also you may want to see my change_data
function as it is connected to why i need the string to be in one line, so here it is:
function change_data($des_line, $data) {
$file = 'config.php';
$lines = file($file);
$i=1;
foreach($lines as $line_num => $line) {
$wiersz[$i] = $line;
$i++;
}
$change = explode("'", $wiersz[$des_line]);
$wiersz[$des_line] = $change[0] . "'" . $data . "'" . $change[2];
$i = 1;
$f = fopen($file, w);
while($i <= count($wiersz)) {
fwrite($f, $wiersz[$i]);
$i++;
}
fclose($f);
header('location: index.php?p=admin');
}
I'm not PHP specialist so sometimes I do things little "hard" way.. I had huge problems with reading file config.php
line by line and these are results of my few-hours effort :(
By default, whenever we press “enter” or “shift+enter” it creates a new line in the text area.
Try the following: <textarea>A <a href='x'>link</a>. </textarea> to see. The P element renders all contiguous white spaces (including new lines) as one space.
In HTML, the <br> element creates a line break.
The <br> HTML element produces a line break in text (carriage-return).
have you tried the php constant PHP_EOL? in you str_replace code?
$open=str_replace(PHP_EOL,"<br>",$_POST["open"]);
There is a ready made PHP function for that named nl2br
Source: https://stackoverflow.com/a/16376133/469161
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With