ajtruckle said:
Hi,I have a question about replacing text. With SQL, if you do a query using %LIKE% it returns case-insensitive results. This is fine.
But the problem is if I then want to display these fields and the chosen word in bold. Take for example:
table
The database will also return "Table"
But I try to use:
str_replace($search_text, "<strong>" . $search_text . "</strong>", $db_field['description']);
it is only show in bold the occurences of "table".
I know there is a str_ireplace function which is case insensitive but really what I want is for a case insensitive serach to be done but for the replaced word to match the original case usage.
Am I making sense?
Andrew
If SQL returns all results than simply by using an array/while loop you can iterate through all of the results and apply the same styling to all results.
I have never used MS-SQL with PHP but with MySQL you would do:
<?
$search_text = $_POST['search'];
$search_text = mysql_real_escape_string($search_text);
$connection = mysql_connect("blah", "blah", "blah");
$query = mysql_query("SELECT id, description FROM db.table WHERE description LIKE '%$search_text%'");
echo "<p>Results for <strong>$search_text</strong></p>";
while ($data = mysql_fetch_array($query)) {
$id = $data['id'];
$desc = $data['description'];
echo "<p>$id - $desc</p>";
}
?>
If this doesn't help please let me know more details about what you're trying to do.
Andy