2016-10-31 59 views
1

我已經創建了一個區域,用戶可以在其中鍵入一些文本並在文本區域添加笑臉。笑臉helperCodeigniter笑臉不會出現在預覽區域

如何當我點擊我的預覽按鈕時,它不顯示生成的笑臉。

enter image description here

問題如何確保在預覽是如果有 問題的任何笑容將在預覽中顯示正常。我在控制器上的預覽功能上使用preg_replace_callback。也.replace對腳本更新我發現this用戶指南,但不知道在何處添加它的代碼

這裏生成的笑容。

<?php 

class Question extends CI_Controller { 

    public $data = array(); 

    public function __construct() { 
     parent::__construct(); 
     $this->load->helper('smiley'); 
    } 

    public function create() { 

     $this->load->library('table'); 

     $image_array = get_clickable_smileys(base_url('assets/img/smiley'), 'question'); 

     $col_array = $this->table->make_columns($image_array, 8); 

     $data['smileys'] = $this->table->generate($col_array); 

     $this->form_validation->set_rules('title', 'title', 'trim|required'); 
     $this->form_validation->set_rules('question', 'question', 'trim|required|callback_question'); 

     if ($this->form_validation->run() == true) { 

     } 

     $data['page'] = 'question/create'; 

     $this->load->view($this->config->item('config_template') . '/template/template_view', $data); 
    } 

    public function preview() { 
     $data = array('success' => false, 'question' => '', 'tag' => ''); 

     if ($_POST) { 
      $string = $this->input->post('question'); 

     $match = array(
      '<' => '&lt;', 
      '>' => '&gt;', 
     ); 

     $new_data = preg_replace_callback("#</?(pre|code|h1|h2|h3|h4|h5|h6|b|strong|i|u|hr)>|[<>]#", function ($match) { 
     return $match[0] == '<' ? '&lt;' : ($match[0] == '>' ? '&gt;' : $match[0]); 
     }, $string); 

      $data['question'] = $new_data; 
      $data['success'] = true; 
     } 

     $this->output 
     ->set_content_type('application/json') 
     ->set_output(json_encode($data)); 
    } 


} 

腳本上查看

<script type="text/javascript"> 
$('#preview-question').on('click', function (e) { 
    $.ajax({ 
     url: "<?php echo base_url('question/preview');?>", 
     type: 'POST', 
     data: { 
      question: $('#question').val(), 
      '<?php echo $this->security->get_csrf_token_name(); ?>' : '<?php echo $this->security->get_csrf_hash(); ?>' 
     }, 
     dataType: 'json', 
     success: function(response){ 
      if (response.success) { 
       $('#preview').html(response.question); 
       if ($("#preview").find("pre").length > 0){ 
        var html = $('#preview pre').html().replace(/</g, "&lt;").replace(/>/g, "&gt;"); 
        $('#preview pre').html(html); 
        $('pre').each(function(i, block) { 
         hljs.highlightBlock(block); 
        }); 
       } 
      } else { 

      } 
     } 

    }); 

    e.preventDefault(); 
}); 

function wrapText(elementID, openTag, closeTag) { 
    var textArea = $('#' + elementID); 
    var len = textArea.val().length; 
    var start = textArea[0].selectionStart; 
    var end = textArea[0].selectionEnd; 
    var selectedText = textArea.val().substring(start, end); 
    var replacement = openTag + selectedText + closeTag; 
    textArea.val(textArea.val().substring(0, start) + replacement + textArea.val().substring(end, len)); 
} 

$(document).ready(function(){ 
    $('#bold').click(function() { 
     wrapText('question', "<strong>", "</strong>"); 
    }); 
    $('#italic').click(function() { 
     wrapText("question", "<i>", "</i>"); 
    }); 
    $('#underline').click(function() { 
     wrapText("question", "<u>", "</u>"); 
    }); 
    $('#pre').click(function() { 
     wrapText("question", "<pre>", "</pre>"); 
    }); 
}); 
</script> 

回答

0

得到它的工作

我不得不使用$data['question'] = parse_smileys($new_data, base_url('assets/img/smiley'));

public function preview() { 
    $data = array('success' => false, 'question' => '', 'tag' => ''); 

    if ($_POST) { 
     $string = $this->input->post('question'); 

     $match = array(
      '<' => '&lt;', 
      '>' => '&gt;', 
     ); 

     $new_data = preg_replace_callback("#</?(pre|code|h1|h2|h3|h4|h5|h6|b|strong|i|u|hr)>|[<>]#", function ($match) { 
     return $match[0] == '<' ? '&lt;' : ($match[0] == '>' ? '&gt;' : $match[0]); 
     }, $string); 

     $data['question'] = parse_smileys($new_data, base_url('assets/img/smiley')); 
     $data['success'] = true; 
    } 

    $this->output 
     ->set_content_type('application/json') 
     ->set_output(json_encode($data)); 
} 

enter image description here