Reputation: 11
i'm passing search variable as array to view file where i'm populating description a column from database how would i highlight certain search keywords in my description on view page.
// as my searched keywords are "mobiles in android".
//my returned description row is as // "There are hundreds of mobiles in android."
// how to highlight mobiles and android.
// which are returned from data base what to do with $row['desc'] preg_replace, str_replace tried, imploded the words tried, i replace tried problem not solved.
My Model:
public function anything($tbl){
$this->db->select('*');
$this->db->from($tbl);
$query = $this->db->get();
return $query->result_array();
}
My controller :
$search = $this->input->get('search');
$tbl = table;
$data['search'] = $this->my_model->search($tbl, $search); //returned query
$this->load->view('search-view', $data);
View File
foreach ($search as $row) {
echo = $row['desc'];
}
Upvotes: 1
Views: 1131
Reputation: 2536
Could you try this..
<?php
$phrase = "There are hundreds of mobiles in android.";
$keywords = array("mobiles", "android");
$replacement = array("<b>MOBILES</b>", "<b>ANDROID</b>");
$newphrase = str_replace($keywords, $replacement, $phrase);
echo $newphrase;
?>
proof: http://sandbox.onlinephpfunctions.com/code/c28d9dfd718c921ef83a7a2ae3c922d70068d6d2
EDIT:
I am working on your code, so the solution above can fit to your code.
So assuming that $row['desc'] is the phrase then it would be like this:
<?php
$phrase = $row['desc'];
$keywords = array("mobiles", "android"); // or you could do the following
$keywords = explode(" ", $this->input->post('keywords'); // Or whatever value it is from POST
$replacement = array("<b>MOBILES</b>", "<b>ANDROID</b>"); // I over simplify things here.
$newphrase = str_replace($keywords, $replacement, $phrase);
echo $newphrase;
?>
Upvotes: 0