Hello dear php-experts,
I am very new to php and I am standing for a bit of code that I cannot understand. The code is supposed to take a user’s search query and highlight it on the page if it is found. To me it seems as if this is like horrible coding, but I would love if someone could tell me in detail what is happening in each step. Thank you so much in advance.
// if the user searches a query
if(isset($http[‘query’]) && $http[‘query’] != “”) {
// first generate the string to be put in the second preg_replace
$replaceWith = “preg_replace(’#\b”. str_replace(’\’, ‘\\’, $http[‘query’]) ."\b#i’, ‘<span class=“queryHighlight”>\\0’,’\0’)";
// the string to be searched in has to be freed from tags first and within this raw text we search with the preg_replace aboth the query string and mark it with the class queryHighlight
$transfersStr = preg_replace(’#(>((?>(([^><]+|(?R))))*<))#se’,$replaceWith,’>’.$transfersStr.’<’);
// Finally we cut the ‘<’ and ‘>’ we added
$transfersStr = str_replace(’"’, ‘"’, substr($transfersStr, 1, -1));}
// and print this string
print $transfersStr;