How can regular expressions be used to highlight specific strings within a PHP code stored as a variable?

Regular expressions can be used in PHP to search for specific strings within a code stored as a variable. To highlight specific strings, you can use the preg_replace() function to replace the matched strings with HTML tags for highlighting. You can use regex patterns to match the specific strings you want to highlight within the code variable.

<?php
$code = '$variable = "Hello World";';
$highlighted_code = preg_replace('/(Hello)/', '<span style="background-color: yellow;">$1</span>', $code);
echo $highlighted_code;
?>