I read a lot of questions here, but did not find an answer, but only got confused more. I have some simple code that implements a search, and output the result from a database to a table. Word search results in large text where these words appear, and my attempts at styling fail because the style is applied to the entire text at once, rather than to individual search words. How can highlight some words in a large text when displaying the result?
<input type="text" name="search_box" id="search_box" class="form-control" placeholder="Search" />
<div class="table-responsive" id="dynamic_content"></div>
<script>
$(document).on('click', '.page-link', function(){
var query = $('#search_box').val();
load_data(page, query);
});
$('#search_box').keyup(function(){
var query = $('#search_box').val();
load_data(1, query);
});
</script>
$query = "
SELECT * FROM journal
";
if($_POST['query'] != ''){
$query .= 'WHERE content LIKE "%'.str_replace(' ', '%', $_POST['query']).'%" ';
}
$query .= 'ORDER BY id DESC ';
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
if($total_data > 0)
{
foreach($result as $row)
{
$output .= '
<table>
<tr>
<td><div class="decoration_date">'.$row["user_date"]. '</div>'.$row["content"].'</td>
</tr>
';
}
}
else
{
$output .= '
<tr>
<td colspan="2">Nothing found</td>
</tr>
</table>
';
}
UPDATE
Ok, I did preg_replace(), and some simple steps, but I still don't understand how to take a $row["content"] from an array, modify it and insert it back into the table.
$wordToFind = $_POST['query'];
$wrap_before = '<span class="highlight">';
$wrap_after = '</span>'; preg_replace("/($wordToFind)/i", "$wrap_before$1$wrap_after", ???);