Download .zip |
Info | Example | View files (6) | Download .zip | Reputation | Support forum | Blog | Links |
Last Updated | Ratings | Unique User Downloads | Download Rankings | |||||
2016-11-25 (11 months ago) | Not enough user ratings | Total: 116 This week: 1 | All time: 8,693 This week: 662 |
Version | License | PHP version | Categories | |||
php-utilities 1.0.0 | BSD License | 5.6 | PHP 5, Language, Parsers |
The PhpUtilities class contains a set of static methods which focus on expression evaluation, PHP parsing and some shell-related features.
You will find below a complete reference about each method. There is also an examples directory where you will find a detailed example for most of the functions described here.
This package depends on the following package : http://www.phpclasses.org/package/9336-PHP-Match-MSDOS-UNIX-patterns-with-regular-expressions.html.
A copy of the source file has been provided here for your convenience, but it may not reflect the latest version.
$status = PhpUtilities::EvaluateExpression ( $expr, &$result, &$error = null ) ;
Evaluates an expression, but guarantees that no message will be output if the expression is incorrect.
This function uses the eval() builtin function.
If the expression is correct, the expression result will be put in the $result parameter and the return value will be true. If the expression is incorrect or if its evaluation generated a notice or error message, the return value will be false and the $error variable will receive the exact error message.
The parameters are the following :
A PHP expression to be evaluated. It can include any PHP code allowed inside an expression and does not need to be terminated with a semicolon. A return keyword will be prepended to the expression you supplied so that you will be able to retrieve the result, and a semicolon will be appended if the supplied expression does not end with a semicolon. Thus, if you supplied the following expression :
17 * 8
the final expression passed to the eval() function will be :
return 17 * 8;
Note that this automatic process of appending a semicolon if the supplied expression does not end with it may alter some error messages in case of a syntax error ; consider the following (incorrect) expression :
17/
The eval() function will issue the following error :
Parse error: syntax error, unexpected end of file
while you will get the following error with the EvaluateExpression method :
Parse error: syntax error, unexpected ';'
This is due to the fact that EvaluateExpression has remodeled your initial expression in the following way before passing it to the eval() function :
return 17/;
result (string) : A variable that will receive the result of the expression evaluation. Beware : this variable will remain unmodified if an error occurred. For this reason, care must be taken when you perform successive calls to EvaluateExpression ; the following code :
$status = PhpUtilities::EvaluateExpression ( '3*2', $result, $error ) ; $status = PhpUtilities::EvaluateExpression ( 'INCORRECT_CONSTANT/', $result, $error ) ;
will leave the variable $result to the value 6 even after the second invocation to EvaluateExpression ; you should always check the return value of the function to avoid such situations.
$result = PhpUtilities::EvaluateTags ( $value, $prepend ) ;
Evaluates a string and replaces all the PHP opening/closing tags with the output of the corresponding code inside. In some sense, it does the following :
ob_start ( ) ;
include ( 'example.ini' ) ;
$contents = ob_get_clean ( ) ;
The main differences between the traditional PHP way and the EvaluateTags* method are :
PHP eats up any line break after a closing tag (?>), thus affecting your document structure. EvaluateTags does not. Consider the following example file, which is a .INI file :
[Settings] HOME = <?= getenv ( 'HOME' ?> File = example.txt
This will produce the following, using the PHP way (assuming that your $HOME variable is set to /users/myself) :
[Setting]
HOME = /users/myselfFile = example.txt
The parameters are the following :
Contents to be evaluated. The following PHP opening/closing tags are recognized :
<?php ... ?>
<?= ... ?>
<? ... ?>
The short open tag (<? ... ?>) will be processed only if the short\_open\_tags directive of your php.ini file is set to on.
public static function ExpandShellParameters ( $string, $values = null ) ;
Expands any shell-like parameter reference in the supplied string with its corresponding value in the $values array.
Parameter references specified in $string can have the following forms :
Each occurrence in the input string will be replaced with its corresponding value in the $values array : $0 will be replaced with element 0, $1 with element 1, and so on.
$0 traditionally contains the program path.
Will be substituted with all parameter values in the $values array, separated by a space.
Will be substituted with parameters x to y, which are optional ; the form "$x-" means "all parameters starting from x up to the last one, while the form "$-y" means : "all parameters up toy*".
Will be substituted with the last value in the $values array.
The function parameters are the following :
The following example prints the string "Hello world" :
echo PhpUtilities::ExpandShellParameters ( "$0 $1", array ( 'Hello', 'world' ) ) ;
public static function GetPHPTokens ( $input, $flags = PHP_TOKENS_DEFAULT ) ;
This function overrides some limitations of the token\_get\_all() builtin function and adds a few extra features :
The function returns an array of associative arrays which have the following keys : - id : token id ; can be any one of the predefined PHP token ids (T\_\, such asT\_WHITESPACE* for example), or one of the following integer constants :
- XT\_LESS\_THAN : the "<" character
- XT\_GREATER\_THAN : ">"
- XT\_QUESTION\_MARK : "?"
- XT\_LEFT\_PARENT : "("
- XT\_RIGHT\_PARENT : ")"
- XT\_COMMA : ","
- XT\_AMPERSAND : "&"
- XT\_TILDE : "~"
- XT\_SHARP : "#". Normally returned as T\_COMMENT by the token_get_all() function
- XT\_DOUBLE\_QUOTE : double quote character
- XT\_SINGLE\_QUOTE : single quote character
- XT\_LEFT\_BRACE : "{"
- XT\_LEFT\_BRACKET : "["
- XT\_DASH : "-"
- XT\_VERTICAL\_BAR : "|"
- XT\_UNDERLINE : "_"
- XT\_BACKSLASH : "\\"
- XT\_CARET : "^"
- XT\_AT\_SIGN : "@"
- XT\_RIGHT\_BRACKET : "]"
- XT\_EQUAL\_SIGN : "="
- XT\_PLUS : "+"
- XT\_RIGHT\_BRACE : "}"
- XT\_PERCENT\_SIGN : "%"
- XT\_STAR : "*"
- XT\_BANG : "!"
- XT\_COLON : ":"
- XT\_SLASH : "/"
- XT\_SEMICOLON : ":"
- XT\_DOT : "."
- XT\_BACKQUOTE : "`"
- XT\_DOLLAR : "$"
- XT\_CATENATE : "##". This is a catenation operator used by some preprocessor, and will only be recognized if the PHP_TOKENS_PREPROCESSOR flag has been specified in the $flags parameter.
- XT\_EOF : Marks the end of the input
- XT\_UNKNOWN : Unknown token found. Such a value is returned when the token\_get\_all() function returns a string, and that string has not been recognized (this should never happen).
Parameters are the following :
public static function GetPHPTokenName ( $value )
This function is similar to the builtin token\_name() function, but also takes into account the XT\_\* constants defined by this package.
public static function ParseCallback ( $value )
Parses a callback specification string. The $value parameter can hold one of the following constructs :
The function returns a callback value, either as an array contain a class name (as a string) or object and a method name, or as a string which represents a function name.
Files |
File | Role | Description | ||
---|---|---|---|---|
examples (3 files) | ||||
PhpUtilities.phpclass | Class | Class source | ||
README.md | Doc. | Documentation | ||
Regex.class.php | Class | Class source |
Files | / | examples |
File | Role | Description |
---|---|---|
evaluateexpression.ini | Data | Auxiliary data |
evaluateexpression.php | Example | Example script |
evaluatetags.php | Example | Example script |
php-utilities-2016-11-25.zip 29KB | |
php-utilities-2016-11-25.tar.gz 26KB | |
Install with Composer |
Needed packages | ||
Class | Download | Why it is needed | Dependency |
---|---|---|---|
PHP Regex Advanced | .zip .tar.gz | Required | Required |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.