PHP解析器工作原理,thinkphp url调度器解析原理

1.什么pathinfo?php

举个例子: http://www.test.com/index.php/type/news/id/4?name=chen&height=33web

1. index.php 是服务器上真实存在的文件名  (在apache没有开启重写这个文件的状况下)正则表达式

2. type/news/id/4 就是pathinfo 会自动保存在$_SERVER['PATH_INFO'] 中thinkphp

3. ?后面的 name=chen&height=33 就是'query'参数 保存在$_SERVER['QUERY_STRING']中,同时用$_GET['name']的方式也能够访问apache

4. thinkphp的url调度器就是把$_SERVER['PATH_INFO']的字符串解析到$_GET,$_POST,$_REQUEST数组中,如上面的解析后:$_GET['type'],$_GET['id']     数组

举个例子:服务器

$_SERVER['PATH_INFO'] = 'type/news/id/4' //解析到$_GET中

preg_replace('/(\w+)\/([^,\/]+)/e','$_GET[\'\\1\']=\'\\2\'',trim($_SERVER['PATH_INFO'],'/'));

5. 在实际部署的时候每每会把index.php文件,只要开启apache的重写模块就好了,具体配置看apache手册

函数

2.pathinfo须要注意什么?url

1.pathinfo 须要服务器的支持(apache是支持的,nigix默认不支持pathinfo)spa

2.主流的web服务器是支持pathinfo,在不知pathinfo的状况下,把tp的url_mode 设置为3,而且把参数写入到

兼容模式的参数中 如:  s=Home/Index/index/id/2  tp解析时第一步就会执行: $_SERVER['PATH_INFO']=$_GET[s];

3.url如何解析?

$_SERVER['PATH_INFO'] 按照tp的格式解析到$_GET,$_REQUEST数组,所用到的技术主要有

1. 处理串处理函数 strpos(找位置)  substr(截取)  strstr(快速截取时用)

2. 正则表达式和及子模式的应用

4.代码示例:

这是一个早期的 PHP 解析器,相当于实现了 PHPPHP 脚本的解析。示例代码:<?php // Autoload required classes require "vendor/autoload.php"; // Instantiate new parser instance $parser = new PhpParser\Parser(); // Return and print an AST from string contents $astNode = $parser->parseSourceFile('<?php /* comment */ echo "hi!"'); var_dump($astNode); // Gets and prints errors from AST Node. The parser handles errors gracefully, // so it can be used in IDE usage scenarios (where code is often incomplete). $errors = PhpParser\Utilities::getDiagnostics($astNode); var_dump(iterator_to_array($errors)); // Traverse all Node descendants of $astNode foreach ($astNode->getDescendantNodes() as $descendant) {     if ($descendant instanceof \PhpParser\Node\StringLiteral) {         // Print the Node text (without whitespace or comments)         var_dump($descendant->getText());         // All Nodes link back to their parents, so it's easy to navigate the tree.         $grandParent = $descendant->getParent()->getParent();         var_dump($grandParent->getNodeKindName());         // The AST is fully-representative, and round-trippable to the original source.         // This enables consumers to build reliable formatting and refactoring tools.         var_dump($grandParent->getLeadingCommentAndWhitespaceText());     }     // In addition to retrieving all children or descendants of a Node,     // Nodes expose properties specific to the Node type.     if ($descendant instanceof \PhpParser\Node\Expression\EchoExpression) {         $echoKeywordStartPosition = $descendant->echoKeyword->getStartPosition();         // To cut down on memory consumption, positions are represented as a single integer          // index into the document, but their line and character positions are easily retrieved.         $lineCharacterPosition = \PhpParser\Utilities::getLineCharacterPositionFromPosition(             $echoKeywordStartPosition         );         echo "line: $lineCharacterPosition->line, character: $lineCharacterPosition->character";     } } 标签:Tolerant
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值