Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

changing new line to <br> in text area

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 &#013 or &#010 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('&#010', '<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] . '&#013';

(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 :(

like image 259
Arkadiusz Galler Avatar asked Oct 12 '13 09:10

Arkadiusz Galler


People also ask

How do you break a line through text area?

By default, whenever we press “enter” or “shift+enter” it creates a new line in the text area.

How do you add a line in textarea?

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.

How do you enter a line break in code?

In HTML, the <br> element creates a line break.

How do I add a line break to a string in HTML?

The <br> HTML element produces a line break in text (carriage-return).


2 Answers

have you tried the php constant PHP_EOL? in you str_replace code?

$open=str_replace(PHP_EOL,"<br>",$_POST["open"]);
like image 81
Cwissy Avatar answered Oct 30 '22 03:10

Cwissy


There is a ready made PHP function for that named nl2br

Source: https://stackoverflow.com/a/16376133/469161

like image 32
Roy Shoa Avatar answered Oct 30 '22 03:10

Roy Shoa