Search for texts in DB without '/ br'

3

I'm working on a CMS that does text editing already in the database, it works as follows: there is a series of inputs[type="radio"] each one of them is a project, and when one of them is clicked appear 4 textareas (via Ajax) with the four texts corresponding to this project. My problem is that in the texts the </ br> entered previously with the nl2br function of PHP, this is a problem because who will edit the texts do not need to see this. I have the following code:

editForm.php:

if(isset($_POST['editTexts'])) {
        if (!empty($_POST['projectIdToEditTexts'])) {

            $id = $_POST['projectIdToEditTexts'];
            $textSeveralToEdit  = nl2br(trim(htmlentities(ucfirst($_POST['editTextSeveral']))));
            $extraTextToEdit  = nl2br(trim(htmlentities(ucfirst($_POST['editExtraTextSeveral']))));
            $textSeveralPTToEdit  = nl2br(trim(htmlentities(ucfirst($_POST['editTextSeveralPT']))));
            $extraTextPTToEdit  = nl2br(trim(htmlentities(ucfirst($_POST['editExtraTextSeveralPT']))));


            $dataBase->updateTextsEdit($id, $textSeveralToEdit, $extraTextToEdit, $textSeveralPTToEdit, $extraTextPTToEdit);
            echo '<p style="text-align:center; color:green;">Success!!<br><br>Text updated!!</p>';

        }
        else {
            echo '<p style="color:red; text-align:center">You must choose a project where to edit the Texts!!</p>';
        }
    }
    ?>
        <form action="" method="POST">
            <h2>Edit texts from project</h2>
            Select the project you want to edit:
            <br>
            <?php
            foreach ($dataBase->fetchAllProjectsAdmin() as $EditButton) {
            ?>
                <input class="radioEditTexts" type="radio" name="projectIdToEditTexts" value="<?php echo $EditButton->id; ?>">
            <?php
            echo '<h4>' .$EditButton->description. '</h4>';
            } ?>
            <br>
            <div id="editTexts">

            </div>
            <br>
            <input type="submit" name="editTexts">
        </form>

......

$('.radioEditTexts').click(function() {

            var viewTexts = $(this).attr('value');

            $.ajax({
                type: 'GET',
                url: '../Ajax/editTexts.php',
                data: {"idProject":viewTexts},
                success: function(data) {
                    $('#editTexts').stop().html(data).hide().fadeIn(500);
                }
            });
        });

editTexts.php

$projectId = $_GET['idProject'];

$getTexts = $dataBase->getDetailsById($projectId)[0];

$mainTextEN = $getTexts->text_several;
$extraTextEN = $getTexts->extra_text;
$mainTextPT = $getTexts->text_severalPT;
$extraTextPT = $getTexts->extra_textPT;

?>
<label>Main Text (PT)?<br><br>
    <textarea name="editTextSeveralPT" cols="50" rows="5"><?php echo $mainTextPT; ?></textarea>
</label>
<br>
<br>
<label>Extra text (PT)?<br><br>
    <textarea name="editExtraTextSeveralPT" cols="50" rows="5"><?php echo $extraTextPT; ?></textarea>
</label>
<br>
<br>
<label>Main Text?<br><br>
    <textarea name="editTextSeveral" cols="50" rows="5"><?php echo $mainTextEN; ?></textarea>
</label>
<br>
<br>
<label>Extra text?<br><br>
    <textarea name="editExtraTextSeveral" cols="50" rows="5"><?php echo $extraTextEN; ?></textarea>
</label>
    
asked by anonymous 16.10.2014 / 16:13

2 answers

6

Assuming I understand correctly, simply reverse what the nl2br () function does.

As there is no native thing, just a simple replacement:

$var = str_replace( array( '<br>', '<br/>', '<br />' ), "\n", $var );

Or a regular, which is shorter. Anything:

$var = preg_replace( '#<br\s*/?>#i', "\n", $var );

A more complete example:

<textarea name="editExtraTextSeveralPT" cols="50" rows="5"><?php echo str_replace( array( '<br>', '<br/>', '<br />' ), "\n", $extraTextPT ); ?></textarea>
    
16.10.2014 / 16:30
1

I already had this problem and I solved it in a different way, instead of saving with nl2br and every time I have to revert to edit and then apply dinovo to save, I decided to save with \ n even in the bank and use nl2br only when the user to read the actual text. I saved work and space in the bank in case of very large texts with many line breaks.

    
25.01.2016 / 13:22