• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Highlight bad words in textarea in php

#1
I have this function below which checks for bad words in callback.


PHP Code:
public function wordcheck() {
 
$disallowed = array('darn''has');

$string $this->input->post('question');

$matches = array();
$matchFound preg_match_all(
               "/\b(" implode($disallowed,"|") . ")\b/i", 
               $string
, 
               $matches
             
);
if (
$matchFound) {

$words array_unique($matches[0]);
 
foreach($words as $word) {
   $bad_word $word; 
}

$this->form_validation->set_message('wordcheck''Your question has a disallowed word ' $bad_word);

return 
false;
} else {

return 
true;
}



On my function ask is there away that it can highlight bad word in my textarea? there is this http://garysieling.github.io/jquery-highlighttextarea/ but it is for javascript/jquery I would like to know if one for php/codeigniter available. 




PHP Code:
public function ask() {
 
$this->form_validation->set_rules('title''title''trim|required');
$this->form_validation->set_rules('question''question''trim|required|callback_wordcheck');

// $this is for HMVC callback enable from my MY_Form_validation
if ($this->form_validation->run($this) == TRUE) {

$question_insert = array(
'title' => $this->input->post('title'true),
'question' => $this->input->post('question'true),
'votes' => '0',
'date_created_on' => time()
);

$this->db->insert('questions'$question_insert);

$question_id $this->db->insert_id();

$questions_category_insert = array(
'question_id' => $question_id,
'category_id' => '2'
);

$this->db->insert('tagged'$questions_category_insert);

redirect(base_url('questions') .'/'$question_id);
}

$data['title'] = $this->input->post('title');

$data['question'] = $this->input->post('question');

$data['category'] = $this->input->post('category');

$data['header'] = Modules::run('catalog/common/header/index');
$data['navbar'] = Modules::run('catalog/common/navbar/index');
$data['footer'] = Modules::run('catalog/common/footer/index');

$this->load->view('theme/default/template/questions/ask'$data);

There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply

#2
Hi,

You are going to have to do it with javascript as you cannot add markup within a text area without it becoming part of the text itself.

The example library you linked to does not work on my browser.

An interesting example approach to this is here: https://codepen.io/lonekorean/pen/gaLEMR

You may be better changing the UI so that the user sees the block of text with words highlighted that are bad in a normal HTML panel, with the text area below. A little bit like how CI forum, when previewing a post, has the preview above and the text area below again.

Best wishes,

Paul.
Reply

#3
(04-07-2017, 05:23 AM)PaulD Wrote: Hi,

You are going to have to do it with javascript as you cannot add markup within a text area without it becoming part of the text itself.

The example library you linked to does not work on my browser.

An interesting example approach to this is here: https://codepen.io/lonekorean/pen/gaLEMR

You may be better changing the UI so that the user sees the block of text with words highlighted that are bad in a normal HTML panel, with the text area below. A little bit like how CI forum, when previewing a post, has the preview above and the text area below again.

Best wishes,

Paul.


Thanks for the reply PaulD I will look into it over weekend.
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.