TextMate PHP Documentation

I have slightly updated M.Spreij's pdoc code to better handle errors and be able to more accurately fetch function info. The code that I have added is really minor so full credit really goes to M.Spreij, (whom I have not been in contact with because i can't really figure out how)

So, after you get M.Spreij's lib.php from here and save it as ~/bin/lib.php you can go ahead and copy the code below and save it as ~/bin/pdoc and set up your textmate commands. Hope this is helpful to someone! send questions to jrgould (at) gmail (dot) com.

php documentation

Here are the commands I've set up:

This will display the function description and arguments as a tooltip
I set the key command for this to ctrl-?

name: pdoc
save: nothing
command: php -f ~/bin/pdoc $TM_CURRENT_WORD
input: Selected Word or Word
output: Show as tooltip

This will append the function arguments to the function
I set the key command for this to ctrl-option-?

name: pdoc replace
save: nothing
command: php -f ~/bin/pdoc snipex $TM_CURRENT_WORD
input: Selected Word or Word
output: Replace Selected Text

here is the updated pdoc code:


#!/usr/bin/php
<?php

/**
 * pdoc fetches the first few lines of syntax for given functions from nl.php.net (in English)
 * Usage: pdoc {function}
 * Crude, but it works after a fashion, for most functions :-)
 * Copyright 2004 M.Spreij <mac.com@nemo> under the GPL: http://www.gnu.org/licenses/gpl.txt
 * 
 * Updated:
 *  2005-08-31: added html_decode() at final print statement
 *  2006-08-12: changes for more consistent functionality by Jeff Gould jrgould (at) gmail (dot) com 
**/

require_once 'lib.php'; # http://dev.expocom.nl/functions.php?id=61

/*if ($argc == 1) {
  echo "  pdoc fetches the first few lines of syntax for given functions from nl.php.net (in English)\n".
       "  Usage: pdoc {function}\n";
  return;
}*/

# Settings
ini_set('display_errors', '1');
ini_set('track_errors', '1');
$func = $argv[1];
if($func == 'snipex'){
   $tab = true;
   $func = $argv[2];
}
ob_start();
$func = preg_replace('/[\(\) ;]/','', $func);
$function = str_replace('_', '-', $func).'.php';
$url      = 'http://us2.php.net/manual/en/function.';

$header  = "Accept-Language: en-gb, en";
$options = array('CURLOPT_HTTPHEADER' => array($header));

$res = fetch_url($url . $function, $options);   

if ($res[0] != 200) {
  if ($res == 302) {
    echo "Function not found";
  }else{
    //echo "Error fetching syntax, ". $res[1];
   print $func;
  }
  //echo "\n";
  //return false;
}else{
  if (strtolower($argv[2])=='w') {
    //echo "Opening $url$function.. \n";
    shell_exec('open '. $url.$function);
    //die();
  }
  $data = $res[1];
}


$start = 'Swedish'; # nice point to start cutting
if($data){
   $data = substr($data, strpos($data, $start) + strlen($start), 1000);
# cuts underneath the table with 'last updated' and stuff
   $data = substr($data, strpos(strtolower($data), '</table>') + 8);
# replace linebreaks with spaces, fix tags, remove nonbreaking spaces
   $data = str_replace(array('&nbsp;', "\n", ' >'), array(' ', ' ', '>'), nl2unix($data));
# insert linebreaks for paragraph and header tags
   $data = str_replace(array('<P>', '</P>'), "\n", $data);
   $data = str_replace(array('<H1>', '</H1>'), "\n", $data);
# cut until the first ')' after the description identifier (that's '<H2>')
   $find = strpos(strtolower($data), '<h2>');
   $find = strpos(strtolower($data), ')', $find + 5);
   $data = substr($data, 0, $find + 1);
# then insert linebreaks for <H2> too
   $data = str_replace(array('<H2>', '</H2>'), "\n", $data);
# remove all the tags and remove double returns
   $data = strip_tags($data);
   $data = trim(str_replace("\n\n", "\n", $data));
   $data = explode("\n", $data);
   if(!$tab){
      $data = $data[2]."\n".$data[4];
      print html_decode($data); # from lib.php
      //echo "\n\n";
   }else{
      $data = $data[4];
      $data = html_decode(substr($data, strpos($data, ' ')+1, strlen($data)));
      print rtrim(ltrim(preg_replace(array('/[\n\r]+/', '/ \(/', ), array('', '$1('),$data)));
   }
}
$output = ob_get_contents();
ob_end_clean();
if( (strlen($output)>strlen($func)) && (strlen($output)<1000)) print $output;
else{
   if(!$tab) print "error: function not found [ $func()]";
   else print $func.'()';
}
?>

or save as here