skip to Main Content

I’m develop a project to create report using PHP and MySQL, but I want to add the WYSIWYG text editor TinyMCE in way to give the user the possibility to add text to the report, I choose this library for the settings and customization of text that offers, so I create a foreach loop to create text areas like this:

 require_once 'queries.php';

 foreach ($parameters as $parameter) {
        echo '<p>Please, write the analysis of '. $parameter['parameter'] .'</p>';
        echo '<textarea name="analysis_'. $parameter['parameter'] .'"></textarea>';
 }

Well the problem when I send the information to the program to generate the text and concatenate with text from my data base I get a line break or margin that I unexpected.

This is a sample of text:

This is a text from data base recovery from PHP, {{ new_text }}

The mark is replace with a PHP function, and when I run the code I got this:

This is a text from data base recovery from PHP,

This is a text add from TinyMCE

But I want to get this:

This a text from data base recovery from PHP, This a text add from TinyMCE

I was trying it to remove the TinyMCE editor, send the text and I got the result that I want, but this eliminate the possibility of write text with styles like bold, subindex and superindex, so I don’t know what is happening when I add the TinyMCE editor and this space appear.

This is the settings that I’ve in my .js script:

tinymce.init({
    selector: 'textarea',
    plugins: 'autoresize',
    toolbar: 'undo redo | bold italic | superscript subscript | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | fontfamily fontsize',
    fontsize_formats: "6pt 8pt 9pt 10pt 12pt 14pt 16pt",
    menubar: false,
    toolbar_location: 'bottom',
    height: 300,
    forced_root_block_attrs : { style: 'text-align: justify;' },
});

I really appreciate your help, cause I don’t know how configure the editor, I was searching a lot but nothing.

I’ll accept if you can recommend a similar editor as well if you had a better experience.

I try concatenate strings from the same PHP and works, remove the editor and works, I though that if I put the mark out of the p tags the problem solve, but this isn’t happen

2

Answers


  1. Chosen as BEST ANSWER

    I found it a simple solution to that issue, if you are using PHP as backend programming language you can use the function preg_replace() and change the p tags in this way:

    $variable = preg_replace(
        "/<ps(.+?)>(.+?)</p>/",
        "<span>$2</span>",
        $_POST["key"]
    );
    

    This is better cause p tags and div tags generate line breaks in case you need concatenate properly the text, you can research what function is similar in you programming languaje to do that, hope this would be a solution for other people.

    This is better cause p tags and div tags generate line breaks in case you need concatenate properly the text, you can research what function is similar in you programming languaje to do that, hope this would be a solution for other people.


  2. You can also try changing how TinyMCE behaves when new content is added, invert or linebreak might work best for controlling new <p> tags. https://www.tiny.cloud/docs/tinymce/6/content-behavior-options/#newline_behavior

    Also, make sure the formatting of your forced root block attribute is correct:

    forced_root_block_attrs: { 'style': 'text-align: justify;' },
    

    EDIT: Documentation, and code snippet from the docs:

    tinymce.init({
      selector: 'textarea',  // change this value according to your HTML
      forced_root_block_attrs: {
        'class': 'myclass',
        'data-something': 'my data'
      }
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search