For everyone who was interested in the CLE stuff

    Date: 07/23/06 (PHP Community)    Keywords: php

    Since I got a few interested replies on this community for the CLE stuff, here it is, the code for taking input and making use of it, either in the middle of the script, or as an option.


    Here's the on-demand input:
    function readline($prompt="") {
      print 
    $prompt$out=""$key="";
      
    $key=fread(STDIN,1);
      while (
    $key!="\n") {
        
    $out.=$key$key=fread(STDIN,1);
      }
      return 
    $out;
    }

    This code can be used in the case of, say we have an exception occur in our script, but we want to check if the user wants to retry, we can do this:
    $continue=readline("Exception has occurred! Would you like to continue? (YES/NO) ");
    Ths script will then pause and wait for input from end-user. Please beware that anytime you use readline (or whatever you rename it to), that the script halts and waits for input in the form of a newline before the function completes. I am still trying to find a way to ignore such input until it is nessicary, and so far all i've found is piggybacking scripts, one with this input code, and the other with the code we want to actively run, connected through proc_open's STDIN and STDOUT.

    And here's the code to take -switches like --help and stuff, as well as alterable=options
    array_shift($argv); $in=$argv;
    if (
    count($in)) {
      foreach (
    $in as $null => $input) {
        if (
    preg_match('/.+[=]{1}.+/',$input)) {
          
    //This is a variable, store it for later
          
    list($key,$data) = explode("=",$input);
          
    $_OPTION["$key"]=$data;
        }
        if (
    preg_match('/[-]{1,2}.+/',$input)) {
          
    //This is a switch, let's go ahead and trigger it
          
    $_SWITCH["{$input}"]=TRUE;
        }
      }
    }

    Example:
    After you've chmodded your_script to be executeable, and added the #!/path/to/php as the first line, you can use it like such.
    "./your_script -stop" will give you $_SWITCH["stop"]==TRUE, and "./your_script option1=variable1" will give $_OPTION["option1"]=="variable1", just to note, the options CAN be encapsulated with quotes to intake an entire string.
    Example: ./your_script option1="full string input can also be done"
    Returns: $_OPTION["option1"]=="full string input can also be done"


    I hope somebody can use this for their projects and make some use out of it all.

    Source: http://community.livejournal.com/php/474193.html

« I need a few pointers... || fopen »


antivirus | apache | asp | blogging | browser | bugtracking | cms | crm | css | database | ebay | ecommerce | google | hosting | html | java | jsp | linux | microsoft | mysql | offshore | offshoring | oscommerce | php | postgresql | programming | rss | security | seo | shopping | software | spam | spyware | sql | technology | templates | tracker | virus | web | xml | yahoo | home