What is the best way to highlight search terms in PHP without changing their case?
When highlighting search terms in PHP without changing their case, you can use the `preg_replace` function with the `i` modifier to perform a case-insensitive search and replace. This allows you to highlight the search terms without altering their original case.
$searchTerm = "example";
$text = "This is an example sentence to demonstrate highlighting search terms in PHP.";
$highlightedText = preg_replace('/\b' . preg_quote($searchTerm, '/') . '\b/i', '<strong>$0</strong>', $text);
echo $highlightedText;
Keywords
Related Questions
- What are the advantages of using mysqli or pdo_mysql over the deprecated mysql extension in PHP for database connectivity?
- In what scenarios would it be preferable to directly integrate PHP functionality into a Java application instead of using system calls?
- Wie könnte das Script verbessert werden, um die Sicherheit zu erhöhen?