What are the potential pitfalls of using eregi_replace in PHP for highlighting search results?

Using eregi_replace in PHP for highlighting search results can lead to potential pitfalls such as deprecated functionality (eregi_replace is deprecated as of PHP 5.3.0), case-insensitive replacements (which may not always be desired), and potential security vulnerabilities (as eregi_replace is not safe for use with user input). To solve this issue, it is recommended to use preg_replace with the 'i' modifier for case-insensitive replacements and to sanitize user input before using it in the replacement.

// Example code snippet using preg_replace for highlighting search results
$search_term = $_GET['search_term']; // Assuming search term is coming from user input

// Sanitize the search term before using it in the replacement
$sanitized_search_term = htmlspecialchars($search_term, ENT_QUOTES, 'UTF-8');

// Highlight search term in the content
$highlighted_content = preg_replace('/' . preg_quote($sanitized_search_term, '/') . '/i', '<span style="background-color: yellow;">$0</span>', $content);

echo $highlighted_content;