PhpDig.net

What is PhpDig?
PhpDig is a PHP MySQL based
Web Spider & Search Engine.




exec

Name

exec — Executes command cmd in the system's command interpreter and returns the last line of output. Optional arguments allow the command output and return value to be captured.

Synopsis

string exec(cmd[, array_name][, $return_value]);
string cmd: Command to be executed
variable array_name (optional): Array to store the command output
variable $return_value (optional): Variable to store the return status of the command

Returns

Last line of output from command cmd

Description

exec()() attempts to execute cmd in the system's command interpreter. PHP waits until the command interpreter returns before execution of the script continues past the call to exec() . If you're using a UNIX-like operating system, you can redirect stdout to a file or/dev/null to avoid this.

If the array_name parameter is set, each line of output from the command will be placed in a separate element in the array. If the array already contains elements, the output will be appended to the end of the array.

If the return_value argument is set, the return value of the command will be stored in this variable.

Version

PHP 3+; PHP 4+

Example

Example 1097. Quick-and-dirty search function using the Windows NT find command

<form>
    Please input a search term and press enter:<br />
    <input type="text" name="term" />
</form>

<?php
function nt_search ($term = "") {
    // Declare my variables
    $max_hits = 0;
    $out = $search_results = "";

    // Run the NT find command - search all files in the current working directory
    exec ("find /C \"$term\" *", $command_output);
    
    // Loop through the lines of output one line at a time
    foreach ($command_output as $line) {
        /*
            Use a regular expression to break the line into a filename and number of results.
            This regex accomplishes the work of at least three other function calls.
            See the chapter on regular expressions for more information.
        */
        ereg ('^-+ (.+): ([0-9]+)$', $line, $capture_buffer);
        list (, $filename, $hits)    = $capture_buffer;

        // Skip over results that had no matches of the term
        if ($hits == 0)
            continue;

        // Convert hits to a ratio of number of results to size of file
        $hits /= filesize ($filename);

        // Keep track of the largest ratio of hits to file size
        $hits > $max_hits
            and $max_hits = $hits;
        
        // Create an array of filenames and search results
        // Note the use of basename inside the brackets after $search_results
        $search_results[basename ($filename)] = $hits;
    }
    
    if (! is_array ($search_results))
        return "Sorry, no matches for the term '$term' could be found.";

    // Sort $search_results by key value, from largest to smallest (aka reverse order)
    arsort ($search_results);
    
    // Loop through the sorted results
    foreach ($search_results as $filename => $hits) {
        // Make the hit ratings relative to the greatest hit rating
        $rating = ceil ($hits/$max_hits * 100);
        $out .= "\n\n<b>$filename</b>\nSearch Rating of of $rating/100";
    }
    
    return ('<b>Your search returned ' . count ($search_results)
        . ' search result(s)</b><blockquote>' . $out . '</blockquote>');
}

if (isset ($term))
    echo '<pre>' . nt_search ($term) . '</pre>';
?>



PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing (Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch). This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/). The authors of this book have elected not to choose any options under the OPL. This online book was obtained from http://www.fooassociates.com/phpfer/ and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4 for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All persons and entities shall have neither liability nor responsibility to any person or entity with respect to any loss or damage arising from the information contained in this book.

Powered by: vBulletin Version 3.0.7
Copyright ©2000 - 2005, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.