title: inserting tinymce content to database
body: <p>I'm trying to insert the content of a textarea that uses tinymce into a mysql database on codeigniter 3.</p>
  
  <p>the view:</p>
  
  <pre><code>&lt;form action=""&gt;
    &lt;textarea id="content" name="content"&gt;&lt;/textarea&gt;
    &lt;input type="submit" name="btn_submit" value="Submit"/&gt;
  &lt;/form&gt;
  &lt;script&gt;
  tinymce.init({
    selector: '#content',
    plugins:  [
      "advlist autolink lists link image charmap print preview anchor",
      "searchreplace visualblocks code fullscreen",
      "insertdatetime media table contextmenu paste jbimages preview emoticons media",
      "textcolor placeholder"
       ],
      toolbar: "bold italic underline strikethrough backcolor emoticons autoresize | bullist numlist outdent indent | insertfile | alignleft aligncenter alignright alignjustify | link media image jbimages undo redo preview",
      menubar:false,
      paste_data_images: true,
      relative_urls: false,
      height : "420",
      paste_word_valid_elements: "b,strong,i,em,h1,h2,u,p,ol,ul,li,a[href],span,color,mark",
      relative_urls : false,
      media_dimensions: false,
      media_poster: false,
      content_css: "/assets/css/bootstrap.min.css",
      setup: function (editor) {
        editor.on('change', function () {
          tinymce.triggerSave();
        });
      }
  });
  &lt;/script&gt;
  </code></pre>
  
  <p>the controller:</p>
  
  <pre><code>$this-&gt;data['content'] = trim($this-&gt;input-&gt;post('content', TRUE));
  $this-&gt;db-&gt;insert('tablename', $this-&gt;data);
  </code></pre>
  
  <p>this is the content field (in database) after inserting:</p>
  
  <pre><code>&lt;p&gt; &lt;/p&gt;
  &lt;p&gt;aaa&lt;/p&gt;
  &lt;p&gt; &lt;/p&gt;
  &lt;p&gt;bb&lt;/p&gt;
  &lt;p&gt; &lt;/p&gt;
  &lt;p&gt; &lt;/p&gt;
  &lt;p&gt; &lt;/p&gt;
  &lt;p&gt;ccc&lt;/p&gt;
  </code></pre>
  
  <p>my questions are:</p>
  
  <ol>
  <li><p>is it OK to save html tags in the database?</p></li>
  <li><p>I want to show the content after replacing all whitespaces with single space. so I tried this:</p></li>
  </ol>
  
  <blockquote>
  <pre><code>$this-&gt;db-&gt;select('content');
  $this-&gt;db-&gt;where('id', 12);
  $content = $this-&gt;db-&gt;get()-&gt;row('content');
  $content = strip_tags($content);
  $content = preg_replace('!\s+!', ' ', $content);
  &lt;div&gt;&lt;?php echo $content; ?&gt;&lt;/div&gt;
  </code></pre>
  </blockquote>
  
  <p>but when I use <code>preg_replace('!\s+!', ' ', $dbfield);</code> it replaces the empty space with a black diamond with question mark in it. what is the correct way to achive the desired result?</p>