I haven't TRIED this out... just wrapped MOST of your code in a function... but it should work. <?php if ( ! function_exists (pretty_url) ) { function pretty_url ($input_string) { $string = strtolower ($string); $rg = "[^a-zA-Z0-9]"; $parts = preg_match_all ("/$rg/", $string, $matches); $high = implode (" ", $matches[0]); $input = $high; $words = explode (" ",$input); foreach ($words as $term) { $string = str_replace ($term, "", $string); } while (strpos ($string, " ")) { $string = str_replace(" ", "-", $string); } $rtn_string = "<a href=\"".$string.".html\">".$string.".html</a>"; return $rtn_string; } } $string = "Here's the name of the page"; $return_string = pretty_url ($string); echo $return_string; ?>