What are the differences between using str_replace() and eregi_replace() for search term highlighting in PHP?
When highlighting search terms in PHP, using str_replace() is a simpler and more efficient option compared to eregi_replace(). str_replace() performs a simple string replacement, which is case-sensitive and does not support regular expressions. On the other hand, eregi_replace() is a case-insensitive function that supports regular expressions, making it more powerful but potentially slower.
// Using str_replace() for search term highlighting
$keyword = "example";
$text = "This is an example sentence.";
$highlighted_text = str_replace($keyword, "<strong>$keyword</strong>", $text);
echo $highlighted_text;
Related questions
-
Why is it important to avoid using deprecated mysql_ functions in PHP and what are the alternatives?deprecated mysql_ functions
-
PHP fgetcsv parsing
-
Modulo Operator PHP Divisible