1. Go to this page and download the library: Download farafiri/php-parsing-tool library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?phprequire_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
farafiri / php-parsing-tool example snippets
$dates = '2012-03-04,2013-02-08,23.06.2012';
$parser = new \ParserGenerator\Parser('start :=> datesList.
datesList :=> date "," datesList
:=> date.
date :=> year "-" month "-" day
:=> day "." month "." year.
year :=> /\d{4}/.
month :=> /\d{2}/.
day :=> /\d{2}/.');
$parsed = $parser->parse($dates);
//and now you want to get list of yearsforeach($parsed->findAll('year') as $year) {
echo $year;
}
//this time you want to print all months form year 2012foreach($parsed->findAll('date') as $date) {
if ((string) $date->findFirst('year') === '2012') {
echo $date->findFirst('month');
}
}
// by adding 'defaultBranchType' with 'PEG' value into options we declare grammar as PEG
$parser = new \ParserGenerator\Parser('start :=> start "x"
:=> "x".', array('defaultBranchType' => 'PEG'));
// but PEG grammar cannot be left recursive, call parse will run infinite loop in this case//You have 2 solutions now//1-st: you can change grammar a bit:
$parser = new \ParserGenerator\Parser('start :=> "x" start
:=> "x".', array('defaultBranchType' => 'PEG'));
//2-nd: use default branch type
$parser = new \ParserGenerator\Parser('start :=> start "x"
:=> "x".');
$nonGreedy = new \ParserGenerator\Parser('start :=> "a"* "a"*.');
$nonGreedy->parse("aaa")->getSubnode(0)->toString(); // "" first "a"* takes nothing
$nonGreedy->parse("aaa")->getSubnode(1)->toString(); // "aaa" so second must consume all left
$greedy = new \ParserGenerator\Parser('start :=> "a"** "a"**.');
$greedy->parse("aaa")->getSubnode(0)->toString(); // "aaa" first "a"** takes all
$greedy->parse("aaa")->getSubnode(1)->toString(); // "" so nothing left for second
$greedy = new \ParserGenerator\Parser('start :=> "a"** "a"+.');
// "aa" "a"** tries to take all but then parsing would fail and he must leave last char for "a"+
$greedy->parse("aaa")->getSubnode(0)->toString();
$greedy->parse("aaa")->getSubnode(1)->toString(); // "a"
$parser = new \ParserGenerator\Parser('start :=> word+",".
word :=> /\w+/.');
foreach($parser->parse("a,bc,d")->getSubnode(0)->getSubnodes() as $subnode) {
echo $subnode . ' ';
} //prints "a , bc , d "foreach($parser->parse("a,bc,d")->getSubnode(0)->getMainNodes() as $subnode) {
echo $subnode . ' ';
} //prints "a bc d "
//parser matching only integers from 3 to 17 (inclusive)
$parser = new \ParserGenerator\Parser('start :=> 3..17 .');
$parser->parse('2'); //false
$parser->parse('18'); //false
$parser->parse('12'); //syntax tree object//parser matching only integers > 0
$parser = new \ParserGenerator\Parser('start :=> 1..infinity .');
//parser matching integers in hex decimal and oct
$parser = new \ParserGenerator\Parser('start :=> -inf..inf/hdo .');
$parser->parse('0x21')->getSubnode(0)->getValue(); // 33
$parser->parse('21')->getSubnode(0)->getValue(); //21
$parser->parse('021')->getSubnode(0)->getValue(); //17//matching month number with leading 0 for < 10
$parser = new \ParserGenerator\Parser('start :=> 01..12 .');
$parser->parse('4'); //false
$parser->parse('04'); //syntax tree object
$parser = new \ParserGenerator\Parser('start :=> (time(Y-m-d) | time(d.m.Y)) .');
$parser->parse('2017-01-02')->getSubnode(0)->getValue(); // equal to new \DateTime('2017-01-02')
$parser->parse('03.05.2014')->getSubnode(0)->getValue(); // equal to new \DateTime('2014-05-03')
$parser = new \ParserGenerator\Parser('start :=> word not is keyword.
word :=> /\w+/.
keyword :=> ("do" | "while" | "if").');
$parser->parse('do'); //false
$parser->parse('doSomething'); // syntax tree object
$parser = new \ParserGenerator\Parser('start :=> word not(is keyword or
is ("p" text /* we don`t want words starting with "p" */) or
is /./ /* we don`t want one letter words */ ).
word :=> /\w+/.
keyword :=> ("do" | "while" | "if").');
$parser->parse('do'); //false
$parser->parse('d'); //false
$parser->parse('post'); //false
$parser->parse('doSomething'); // syntax tree object
$parser = new \ParserGenerator\Parser('start :=> unorder("", ?"a", ?"b").');
$parser->parse('a'); //syntax tree object
$parser->parse('b'); //syntax tree object
$parser->parse(''); //false
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.