Auto-link to php.net from function names in text
Posted: Tue Jul 12, 2005 12:29 pm
I got bored today at work so threw this together. Perhaps it could be implemented to make a phpBB mod or something or other although it would need work to avoid BBCode clashes (it might go nicely in a PHP Blog too
). I dunno, if you want it, here's what it does. It gathers the PHP function info into a database and then checks given HTML/Text for matches of the function names and converts them to hyperlinks to the php.net manual page for the function. It also adds a "title" attribute containing the function usage - a *cough* cheap tooltip 
----------
Stage 1 (one-off data extraction - update when new php versions are released if you want):
This stage rips information from the PHP manual download. It extracts all the function names and the description on how they are used (i.e. array_keys (array array)) and dumps the data into a mysql table. You need to give it your database info as arguments and make sure you DON'T have table named `phpfuncs` in there already. Make sure you get the "multiple HTML files" version of the manual and extract it somewhere PHP has access to read.
You can get the multiple file version here: http://uk.php.net/get/php_manual_en.tar ... his/mirror (2005-07-12)
$path is the path to the manual files
$user, $pass, $db, and $server are the MySQL database credentials.
You need only run this function once.
Stage 2 (Either before adding to DB or when displaying on web page):
This searches given text and adds the code required to show hyperlinks and tooltips. It's not slow but it's probably best to do it before storing the data into a database.
It takes $conn as an argument which should be an established MySQL connection to the database with the database selected ready for use.
----------
Hmm... well it was something to do 
----------
Stage 1 (one-off data extraction - update when new php versions are released if you want):
This stage rips information from the PHP manual download. It extracts all the function names and the description on how they are used (i.e. array_keys (array array)) and dumps the data into a mysql table. You need to give it your database info as arguments and make sure you DON'T have table named `phpfuncs` in there already. Make sure you get the "multiple HTML files" version of the manual and extract it somewhere PHP has access to read.
You can get the multiple file version here: http://uk.php.net/get/php_manual_en.tar ... his/mirror (2005-07-12)
$path is the path to the manual files
$user, $pass, $db, and $server are the MySQL database credentials.
You need only run this function once.
Code: Select all
<?php
/*
Written by d11wtq of http://forums.devnetwork.net
Grabs details of ALL the documented functions for PHP
-- using the latest HTML download version of the manual
-- (multiple files version).
The data is dumped into a given database in a table named
-- `phpfuncs`.
*/
//This could take some time, better to increase the time limit
set_time_limit(100);
//Path requires the trailing FORWARD slash (I couldn't be bothered to check for it)
function grabNdumpPHPFuncs($path, $user, $pass, $db, $server='localhost') {
$conn = mysql_connect($server, $user, $pass) or die ('Error connecting to database.');
mysql_select_db($db, $conn) or die ('Error selecting database.');
//Create a new table ready for the insert
$create_query = "e;
CREATE TABLE `phpfuncs` (
`id` int(4) auto_increment,
`fname` varchar(30) not null,
`fdesc` varchar(120) not null,
PRIMARY KEY(`id`)
)
"e;;
mysql_query($create_query) or die (mysql_error());
if (is_dir($path)) {
if (false === $handle = opendir($path)) {
die ('Error opening directory, does PHP have permission?');
} //End if
$count = 0;
while (false !== $file = readdir($handle)) {
if (preg_match('/^function\.(.*?)\.html$/', $file, $m)) {
$name = str_replace('-', '_', $mї1]); //file-get-contents => file_get_contents
$htmlData = file_get_contents($path.$file);
preg_match('#><H2\s*>Description</H2\s*>.*?<B\s*CLASS="e;methodname"e;\s*>(.*?)</B\s*>(.*?)<BR#is', $htmlData, $n); //Extract the usage part
if (!isset($nї1])) $nї1] = '';
if (!isset($nї2])) $nї2] = '';
$desc = $nї1].$nї2]; //Rebuild the function usage info
$insert_query = "e;
INSERT INTO `phpfuncs` (
`fname`,
`fdesc`
) VALUES (
'$name',
'$desc'
)
"e;;
mysql_query($insert_query) or die(mysql_error()); //Add the row
$count++;
} //End if
} //End while
closedir($handle); //Finish reading the dir
} else {
die ($path.' does not appear to be a valid path.');
} //End if
mysql_close($conn);
echo 'Done processing '.$count.' records.<br />';
return true;
}
/* EOF */
?>This searches given text and adds the code required to show hyperlinks and tooltips. It's not slow but it's probably best to do it before storing the data into a database.
It takes $conn as an argument which should be an established MySQL connection to the database with the database selected ready for use.
----------
Code: Select all
<?php
/*
Written by d11wtq of http://forums.devnetwork.net/
For this to work you must first have run the
-- grabNdumpPHPFuncs() function to create the database.
Converts plain text PHP function names into hyperlinks
-- to the relevant manual page.
$conn should be the resource ID for the connection
-- to the database containing the `phpfuncs` table.
*/
function PHPFunction2Hyperlink($text, $conn) {
$query = "e;
SELECT
`fname`,
`fdesc`
FROM
`phpfuncs`"e;;
$result = mysql_query($query, $conn) or die (mysql_error());
$tokens = preg_split('/\b/', $text); //Tokenize the regex way
$stash = array();
while ($values = mysql_fetch_assoc($result)) {
$stashї$valuesї'fname']] = $valuesї'fdesc'];
} //End while
foreach ($tokens as $i => $v) {
if (array_key_exists($v, $stash)) {
if (isset($tokensї$i+1])) {
if (preg_match('/\(\)(\s*)/', $tokensї$i+1], $m)) { //The parens are immediately next in queue
$tokensї$i] .= $tokensї$i+1]; //Combine
unset($tokensї$i+1]); //Remove next item
} //End if
} //End if
//Make our nice link to php.net
$tokensї$i] = '<a href="e;http://www.php.net/'.$v.'"e; title="e;'.$stashї$v].'"e;>'.rtrim($tokensї$i]).'</a>'.$mї1];
} //End if
} //End foreach
return implode('', $tokens); //Rebuild the string
}
/* EOF */
?>