cvs: phpweb /include manual-lookup.inc

From: Date: Tue, 07 Jan 2003 22:05:00 +0000
Subject: cvs: phpweb /include manual-lookup.inc
Groups: php.mirrors 
Request: Send a blank email to php-mirrors+get-14830@lists.php.net to get a copy of this message
goba Tue Jan 7 17:05:00 2003 EDT Modified files: /phpweb/include manual-lookup.inc Log: Reworked code. Better variable names, comments added, etc. Functionality not changed in any way, but modern code structures are used ;) ie. foreach instead of while(each())... Index: phpweb/include/manual-lookup.inc diff -u phpweb/include/manual-lookup.inc:1.12 phpweb/include/manual-lookup.inc:1.13 --- phpweb/include/manual-lookup.inc:1.12 Sat Mar 23 05:41:58 2002 +++ phpweb/include/manual-lookup.inc Tue Jan 7 17:05:00 2003 @@ -1,49 +1,71 @@ <?php // -*- C++ -*- -function tryprefix($cc, $func, $prefix) { +// Try to find some variations of keyword with $prefix in the $lang manual +function tryprefix($lang, $keyword, $prefix) +{ global $DOCUMENT_ROOT; - // Get the country code, to find native - // language manual entries first - $try = array(); - - $func = ereg_replace("_","-",$func); - $func = ereg_replace('\(.*\)',"-",$func); - - $try = "/manual/${cc}/${prefix}${func}.php"; - if (@file_exists("$DOCUMENT_ROOT$try")) return $try; - - $nosp = ereg_replace(" ", "", $func); - if ($nosp != $func) { - $try = "/manual/${cc}/${prefix}${nosp}.php"; - if (@file_exists("$DOCUMENT_ROOT$try")) return $try; + // Replace all underscores with hyphens (phpdoc convention) + $keyword = str_replace("_", "-", $keyword); + + // Replace everything in parentheses with a hyphen + $keyword = preg_replace("!\\(.*\\)!", "-", $keyword); + + // Try the keyword with the prefix + $try = "/manual/${lang}/${prefix}${keyword}.php"; + if (@file_exists("$DOCUMENT_ROOT$try")) { return $try; } + + // Drop out spaces, and try that keyword (if different) + $nosp = str_replace(" ", "", $keyword); + if ($nosp != $keyword) { + $try = "/manual/${lang}/${prefix}${nosp}.php"; + if (@file_exists("$DOCUMENT_ROOT$try")) { return $try; } } - $dasp = ereg_replace(" ", "-", $func); - if ($dasp != $func) { - $try = "/manual/${cc}/${prefix}${dasp}.php"; - if (@file_exists("$DOCUMENT_ROOT$try")) return $try; + + // Replace spaces with hyphens, and try that (if different) + $dasp = str_replace(" ", "-", $keyword); + if ($dasp != $keyword) { + $try = "/manual/${lang}/${prefix}${dasp}.php"; + if (@file_exists("$DOCUMENT_ROOT$try")) { return $try; } } - $noul = ereg_replace("-", "", $func); - if ($noul != $func) { - $try = "/manual/${cc}/${prefix}${noul}.php"; - if (@file_exists("$DOCUMENT_ROOT$try")) return $try; + + // Remove hyphens (and underscores), and try that (if different) + $noul = str_replace("-", "", $keyword); + if ($noul != $keyword) { + $try = "/manual/${lang}/${prefix}${noul}.php"; + if (@file_exists("$DOCUMENT_ROOT$try")) { return $try; } } + + // Nothing found return ""; } -function find_manual_page($lang, $function) { - $sections = array("", "ref.", "function.", "class.", "feature-", "control-structures.", "language."); - while (list($t,$section) = each($sections)) { - $try = tryprefix($lang, $function, $section); - if ($try) return $try; +// Try to find a manual page in a specified +// language for the specified "keyword" +function find_manual_page($lang, $keyword) +{ + // Possible prefixes to test + $sections = array( + "", "ref.", "function.", "class.", + "feature-", "control-structures.", "language." + ); + + // Try to find a manual page with the specified prefix + foreach ($sections as $section) { + $found = tryprefix($lang, $keyword, $section); + if ($found) { return $found; } } + + // Fall back to English, if the language was not English, + // and nothing was found so far for any of the prefixes if ($lang != "en") { - reset($sections); - while (list($t,$section) = each($sections)) { - $try = tryprefix("en", $function, $section); - if ($try) return $try; - } + foreach ($sections as $section) { + $found = tryprefix("en", $keyword, $section); + if ($found) { return $found; } + } } + + // Nothing found return ""; }

« previous php.mirrors (#14830) next »