Search text in DB without '</ br>'

Asked

Viewed 107 times

3

I am working on a CMS that does text editing already in the database, this works as follows: there are a number of inputs[type="radio"] each one of them is a project, and when one of them is clickado appear 4 textareas (via Ajax) with the four texts corresponding to that project. My problem is that in the texts appear the </ br> previously inserted with the function nl2br of PHP, this is a problem because who will edit the texts do not need to see it. 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>

2 answers

6


Assuming I have understood correctly, just reverse what the nl2br() function does.

Since there is no native thing, just a simple substitution:

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

Or a regular, which is shorter. Whatever:

$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>
  • Obgado, but I do not want to remove the '</ br>' I just want them not to be shown in the editing form, I just wanted to change line.

  • ex: http://www.lisaportugal.pt/Ao_Ritmo_Da_Produ%C3%87%C3%83o. If you want to change text below the video you would not like that in the textarea where you were editing appears the '</ br>', just change line. I do not want to exclude the possibility of the user changing line (nl2r).

  • 2

    This is just a visual aid. Yes, you will be removing the <br /> from within the TEXTAREAS, but instead of them there will be line breaks ( n). When the form is submitted for editing, as long as you have another nl2br() equal in the routine that processes the inclusion of a new field in the database <br /> will remain present.

  • Ha sorry yes, I spoke without thinking... I understood, correct this would be the expected behavior however: http://iwanttobesanta.com/hey.png. I do not understand why. I also tried with 'preg_replace'

  • If you refer to "Extra text", no. Note that you opted for str_replace() but removed the <br />. You have to replace them with a line break ( n) otherwise processing this form nl2br(0 will have nothing to work with. In addition to making the text stay visually all glued within the TEXTAREA.

  • Perfect, I apologize for my rashness

  • No problem, I’m glad I could help :)

  • Some <br> in your code would be welcome ;) http://i.stack.Imgur.com/Htmzz.png @Bruno

  • Like this, @brasofilo?

  • I edited the answer to demonstrate, it’s nice to try to avoid the horizontal scrollbar (in my snapshot shows the plugin I use to expand code with scrollbar). But that’s not part of no rules for formatting, feel free to reverse. . . . but I think I’ll put the suggestion there in the guide...

  • I understood, but in this particular case I will only revert because it is a TEXTAREA that keeps the indentation spaces as part of the content. In fact I had even written this way, indented, and changed afterwards.

Show 6 more comments

1

I already had this problem and solved it differently, instead of saving with nl2br and every time having q revert to edit and then apply dinovo to save, I decided to save with n same in the bank and use nl2br only when the user was going to read the text in fact. I saved work and space in the bank in the case of very large texts with many line breaks.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.