我正在尝试将使用tinymce的textarea的内容插入到codeigniter 3上的mysql数据库中。
观点:
<form action="">
<textarea id="content" name="content"></textarea>
<input type="submit" name="btn_submit" value="Submit"/>
</form>
<script>
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();
});
}
});
</script>
控制器:
$this->data['content'] = trim($this->input->post('content', TRUE));
$this->db->insert('tablename', $this->data);
这是插入后的内容字段(在数据库中):
<p> </p>
<p>aaa</p>
<p> </p>
<p>bb</p>
<p> </p>
<p> </p>
<p> </p>
<p>ccc</p>
我的问题是:
可以在数据库中保存html标签吗?
我希望在用单个空格替换所有空格后显示内容。所以我试过了:
$this->db->select('content'); $this->db->where('id', 12); $content = $this->db->get()->row('content'); $content = strip_tags($content); $content = preg_replace('!\s+!', ' ', $content); <div><?php echo $content; ?></div>
但是当我使用preg_replace('!\s+!', ' ', $dbfield);
时,它会用一个带有问号的黑色钻石替换空白区域。达到预期结果的正确方法是什么?