Overview
KB
Technical FAQ
PHP Manual
CSS2 Manual
HTML Manual
JS Guide
JS Reference
PhpDock Manual
Nu-Coder Manual
PhpExpress Manual
PHP Joomla
Development
Learn PHP
 
<Chinese, Japanese and other "wide" charactersWAMP and debugger module issue>
Last updated: Sat, 13 Jul 2013

TIP: Some useful scripts for PhpED

PhpED doesn't have real scripting support, but you can do quite a lot by calling an external program and letting it work on the selected text in the current window. Here are a few scripts written i PHP5 that I find useful. If anybody else has written any good scripts, perhaps you could also post them to this thread.

To install any of these scripts, save the code in a .php file somewhere. Then under Settings / Tools / Integration create a new menu with a suitable name. In the Command parameters, set Execute with to Shell, Command line to what's indicated in the posts below. Then check Show this command in Tools menu, Work with editor, Take input from, and Selected text.

Command line: @php5@ -n -f c:\MySource\PhpED\AlignEquals.php
/**
 * Align the rightmost equal signs in the selected text. Useful for formatting
 * assignment lists and/or array assignments.
 */
class AlignEquals
{
    private $text;

    public function run()
    {
        // Needed because file doesn't work with php://stdin
        $this->text = explode("\n", rtrim(file_get_contents('php://stdin')));

        $this->align($this->find_maxpos());

        echo utf8_decode(implode("\n", $this->text));
    }

    private function align($pos)
    {
        for ($i = 0; $i < count($this->text); $i++) {
            $line = $this->text[$i];
            $curpos = strrpos($line, '=');
            if ( $curpos !== FALSE ) 
              $this->text[$i] = 
              str_pad(substr($line, 0, $curpos-1), $pos) . '=' . substr($line, $curpos+1);
        }
    }

    private function find_maxpos()
    {
        $pos = 0;
        foreach ($this->text as $line) {
            $curpos = strrpos($line, '=');
            if ($curpos > $pos) $pos = $curpos;
        }

        return $pos;
    }
}

$ae = new AlignEquals;
$ae->run();

Command line: @php5@ -n -f c:\MySource\PhpED\DoxyComment.php
/**
 * Generates a JavaDoc-style comment for the selected class, function, or
 * variable.
 */
class DoxyComment
{
    private $text;
    private $indent;

    public function run()
    {
        $this->text = rtrim(file_get_contents('php://stdin'));

        $this->find_indent();

        echo utf8_decode($this->create_comment());
    }

    private function find_indent()
    {
        $text = ltrim($this->text);
        $this->indent = strlen($this->text) - strlen($text);
    }

    private function create_comment()
    {
        $pad = str_pad('', $this->indent);

        $abstract = $static = false;
        $access = '';
        $params   = array();

        $new_text  = "$pad/**\n";
        $new_text .= "$pad * Comment\n";

        if (preg_match(
          '/(abstract )?(public |protected |private )?(static )?function \w+\((.*)*?\)/', 
          $this->text, 
          $matches)) {
            // Functions
            $abstract = !empty($matches[1]);
            $static = !empty($matches[3]);
            $access = $matches[2];
            $params = preg_split('/,\s*/', $matches[4], -1, PREG_SPLIT_NO_EMPTY);
        } elseif (preg_match('/(public |protected |private )?(static )?\$\w+/', 
                             $this->text, $matches)) {
            // Variables
            $static = !empty($matches[2]);
            $access = $matches[1];
        } elseif (preg_match('/(abstract )?class \w+/', $this->text, $matches)) {
            // Classes
            $abstract = empty($matches[1]);
        }

        if (count($params) > 0) {
            foreach ($params as $param) {
                $pos = strpos($param, '=');
                if ($pos) $param = trim(substr($param, 0, $pos-1));
                $new_text .= "$pad * @param $param [type] Description\n";
            }
        }
        if ($abstract) $new_text .= "$pad * @abstract\n";
        if ($static) $new_text .= "$pad * @static\n";
        if (!empty($access)) $new_text .= "$pad * @$access\n";

        $new_text .= "$pad */\n";

        return $new_text . $this->text;
    }

}

$dc = new DoxyComment;
$dc->run();

Command line: @php5@ -n -f c:\MySource\PhpED\WrapLines.php 78
... or whatever you want as right margin.
/**
 * Wordwrap the selected text to the given right margin. Keeps the existing
 * right indent and comment prefix. Removes all extra whitespace, so it is
 * only suitable for plain text, not code samples and such.
 */
class WrapLines
{
    private $text;
    private $right_margin, $left_margin;
    private $comment_start = '', $comment_mid = '', $comment_end = '';

    public function run($right_margin)
    {
        $this->text = file_get_contents('php://stdin');
        $this->right_margin = $right_margin;

        $this->find_left_margin();
        $this->find_comment_prefix();
        $this->clean_text();

        echo utf8_decode($this->rewrap_lines());
    }

    private function find_left_margin()
    {
        $text = ltrim($this->text, " \t");
        $this->left_margin = strlen($this->text) - strlen($text);
        $this->text = rtrim($text);
    }

    private function find_comment_prefix()
    {
        if (preg_match('!^(/\*+|\*+ |//+ )!', $this->text, $matches)) {
            $this->comment_start = $matches[1];
            if (strstr($this->comment_start, '/*')) {
                $this->comment_mid = ' * ';
                $this->comment_end = ' */';
            } else {
                $this->comment_mid = $this->comment_start;
            }
        }
    }

    private function clean_text()
    {
        $comments = array();
        if ($this->comment_start != '') $comments[] = $this->comment_start;
        if ($this->comment_mid != '')   $comments[] = $this->comment_mid;
        if ($this->comment_end != '')   $comments[] = $this->comment_end;
        $this->text = 
          trim(preg_replace('/[\r\n ]+/', ' ', str_replace($comments, ' ', $this->text)));
    }

    private function rewrap_lines()
    {
        $wrap_len = $this->right_margin - $this->left_margin - strlen($this->comment_mid);
        $padding  = str_pad('', $this->left_margin);
        return $padding . $this->comment_start
          .(strstr($this->comment_start, '/*') ? "\n$padding" . $this->comment_mid : '')
          .wordwrap($this->text, $wrap_len, "\n$padding".$this->comment_mid)
          .(strstr($this->comment_start, '/*') ? "\n$padding" . $this->comment_end : '');
    }
}

$argv = $_SERVER['argv'];
$wl = new WrapLines;
$wl->run($argv[1]);




<Chinese, Japanese and other "wide" charactersWAMP and debugger module issue>
Last updated: Sat, 13 Jul 2013