#native_company# #native_desc#
#native_cta#

Search the PHP-documentation off-line

By Johan Nsholm
on October 26, 2000

Version: 1.0

Type: Full Script

Category: HTML

License: GNU General Public License

Description: An ugly hack that searches the php-documentation. The script includes the selected/found file and makes some replacements in it so that the search form is always presented. I guess the official search engine at php.net is better, but since I only have modem access I feel reluctant to connect everytime I need to check the docs 🙂

<?
/*

	An ugly hack that searches the php-documentation. The script includes the selected/
	found file and makes some replacements in it so that the search form is always
	presented. I guess the official search engine at php.net is better, but since I only
	have modem access I feel reluctant to connect everytime I need to check the docs :)
	
	There _are_ bugs in this script, but it workes good enought to be usable.


	Enjoy! Email Comments to [email protected]
	Johan Nsholm [www.nasholm.com]

*/

	// ROOT_DOC_PATH is the path to this script
	define("ROOT_DOC_PATH", "/home/httpd/html/dev");
	// HTML_DOC_PATH is the path to the docs
	define("HTML_DOC_PATH", "/php-docs/");

	function PrintHead()
	{
		echo "<html>n";
		echo "	<head>n";
		echo "		<title>Search the PHP-documentation</title>n";
		echo "	</head>n";
		echo "	<body bgcolor='white'>n";
	}

	function PrintTail()
	{
		echo "	</body>n";
		echo "</html>n";
	}

	// Remove rubbish from the file name. Does not work very well
	function StripName($name)
	{
		$name = substr($name, strlen(HTML_DOC_PATH));
		if (strpos($name, "function.") >= 0)
			$name = substr($name, strlen("function."));

		if (strpos($name, ".html") >= 0)
			$name = substr($name, 0, strlen($name) - strlen(".html") -1);

		return $name;
	}

	function PrintForm()
	{
		echo "<form action='phpsearch.php'>";
		echo "Enter function name: ";
		echo "<input type='text' name='function'>&nbsp;&nbsp;";
		echo "<input type='submit'>";
		echo "&nbsp;&nbsp;<a href='phpsearch.php'>Main menu</a>";
	}

	function ReadHTMLFile($fileName)
	{
		echo "<hr size=3 noshade>";
		$fp = fopen($fileName, "r");
		while (!feof($fp)) {
			$line = fgets($fp, 1024);
			$line = str_replace("HREF="", "HREF="phpsearch.php?file=".HTML_DOC_PATH, $line);
			echo $line;		
		}
		fclose($fp);
	}

	if (isset($function)) {
		$hitFiles = array();
		
		$cmd = "find ".ROOT_DOC_PATH.HTML_DOC_PATH."* | grep ".$function;
		$fp = popen ($cmd, "r");
		while (!feof($fp)) {
			$buffer = fgets($fp, 1024);
			
			$file = substr($buffer, strlen(ROOT_DOC_PATH));
			if (strlen($file) > 0) {
				$hitFiles[] = $file;
			}
		}
		pclose($fp);

		$numFound = sizeof($hitFiles);
		if ($numFound == 0) {
			echo "<h2>PHP-function quick search</h2>";
			PrintHead();
			echo "The function <b>$function</b> could not be found";
			PrintForm();
			PrintTail();
		} else if ($numFound == 1) {
			PrintHead();
			echo "<h2>PHP-function quick search</h2>";
			PrintForm();
			ReadHTMLFile(substr(ROOT_DOC_PATH.$hitFiles[0], 0, strlen(ROOT_DOC_PATH.$hitFiles[0])-1));
		} else {
			PrintHead();
			echo "<h2>PHP-function quick search</h2>";
			PrintForm();
			echo "<hr size=3 noshade>";
			echo "$numFound hits<p>";
			for ($i = 0; $i < $numFound; $i++) {
				$file = str_replace($function, "<b>$function</b>", $hitFiles[$i]);
				echo "<a href='phpsearch.php?file=".$hitFiles[$i]."'>".StripName($file)."</a><br>";
			}
			PrintTail();
		}
	} else if (isset($file)) {
		PrintHead();
		echo "<h2>PHP-function quick search</h2>";
		PrintForm();
		ReadHTMLFile(ROOT_DOC_PATH.$file);
	} else {
		PrintHead();
		echo "<h2>PHP-function quick search</h2>";
		PrintForm();
		ReadHTMLFile(ROOT_DOC_PATH.HTML_DOC_PATH."manual.html");
		PrintTail();
	}
?>