Advanced parsing function with whitespace ignoring
This function should be able to parse a specific text to extract variables, allowing any type of text to be used as the parsing maks and to allow the use of a certain wildcard text to represent whitespaces.
Blueprint information
Whiteboard
The function prototype should be like this:
parsetext_
(String) $parseable_text ,
(String) $parsing_mask ,
(Array) $variable_names ,
(String) $whitespace_text,
(Boolean) $match_case = false
)
If the string matches the mask, then the function should return an associative array of each of the variable names with their corresponding values. Even if $match_case is set to false, variable names should have the same case in the variable_names array and the parsing mask.
If $whitespace_text is set to a blank string or left null, all spaces will be parsed as text.
If there is a mismatch, the function returns false.
NOTE: If two variables are declared adjacent to each other without a whitespace or a string to separate them, the function will automatically return false, as it cannot determine where one ends and the other begins.
Example:
$parseable_text = ' ID : 123 ; fRoM : "taBla1" '
$parsing_mask = '&&ID&&
$variable_names = array ( '{ID}' , '{TABLE_NAME}' )
$whitespace
$match_case = false
Returned value of the function:
array ( '{ID}' => 123 , '{TABLE_NAME}' => 'taBla1' )
How the function will work for the example provided:
STEP ONE:
Dismantle the parsing mask into an array of all the string components to look for.
array (
array ( 'type' => 'whitespace' ),
array ( 'type' => 'string' , 'content' => 'ID' ),
array ( 'type' => 'whitespace' ),
array ( 'type' => 'string' , 'content' => ':' ),
array ( 'type' => 'whitespace' ),
array ( 'type' => 'variable' , 'name' => '{ID}' ),
array ( 'type' => 'whitespace' ),
array ( 'type' => 'string' , 'content' => 'FROM' ),
array ( 'type' => 'whitespace' ),
array ( 'type' => 'string' , 'content' => ':' ),
array ( 'type' => 'whitespace' ),
array ( 'type' => 'string' , 'content' => '"' ),
array ( 'type' => 'variable' , 'name' => '{TABLE_NAME}' ),
array ( 'type' => 'string' , 'content' => '"' ),
array ( 'type' => 'whitespace' ),
)
STEP TWO:
Use a finite state machine to parse the text and extract all the variables.
Work Items
Dependency tree
* Blueprints in grey have been implemented.