Thanks in advance. My sincere apologies for the confusion <?php function highlight_search_keywords($text) { $url="http://www.google.com/search?q=keyword&meta="; @extract(@parse_url($url)); if (preg_match('/(google|msn|yahoo)/i', $host) AND preg_match('/[qp]=([^&]+)/i', $query, $keywords)) { $keywords = preg_split('/[\s\+\,]+/', rawurldecode($keywords[1]), -1, PREG_SPLIT_NO_EMPTY); foreach (array_unique($keywords) AS $keyword) { $text = preg_replace('/('. preg_quote($keyword) .')/i', '<span style="color: red; font-weight: bold;">$1</span>', $text); } } return $text; } echo ("welcome home to the keyword"); $page = $_SERVER['PHP_SELF']; $fp = fopen($page, "r") or die("Could not contant $page"); while($new_text = fread($fp,100)){ $text =$new_text; } fclose($fp); echo highlight_search_keywords($text); ?> PHP:
<?php # object buffers are your friend :) ob_start(); function highlight_search_keywords($text) { $url="http://www.google.com/search?q=keyword&meta="; @extract(@parse_url($url)); if (preg_match('/(google|msn|yahoo)/i', $host) AND preg_match('/[qp]=([^&]+)/i', $query, $keywords)) { $keywords = preg_split('/[\s\+\,]+/', rawurldecode($keywords[1]), -1, PREG_SPLIT_NO_EMPTY); foreach ( array_unique($keywords) AS $keyword ) { $text = preg_replace('/('. preg_quote($keyword) .')/i', '<span style="color: red; font-weight: bold;">$1</span>', $text); } } return $text; } # ALL HTML CONTENT echo "welcome home to the keyword"; # NO MORE HTML AFTER HERE $final_page = highlight_search_keywords( ob_get_contents() ); # clean the object buffer ob_end_clean(); # echo the page with function executed on .... echo $final_page; ?> PHP: http://php-learner.com/p.php Fixed