欢迎来到个人简历网!永久域名:gerenjianli.cn (个人简历全拼+cn)
当前位置:首页 > 范文大全 > 实用文>PHP解析类实例

PHP解析类实例

2024-03-25 07:53:17 收藏本文 下载本文

“山木”通过精心收集,向本站投稿了3篇PHP解析类实例,下面小编为大家整理后的PHP解析类实例,欢迎阅读与借鉴!

PHP解析类实例

篇1:PHP解析类实例

作者:mckee 字体:[增加 减小] 类型:

<?phpclass template { private $vars = array; private $conf = ‘‘; private $tpl_name = ‘index‘; //如果模板不存在 会查找当前 controller默认index模板 private $tpl_suffix = ‘.html‘;//如果CONFIG没配置默认后缀 则显示 private $tpl_compile_suffix= ‘.tpl.php‘;//编译模板路径 private $template_tag_left = ‘<{‘;//模板左标签 private $template_tag_right = ‘}>‘;//模板右标签 private $template_c = ‘‘;//编译目录 private $template_path = ‘‘;//模板完整路径 private $template_name = ‘‘;//模板名称 index.html //定义每个模板的标签的元素 private $tag_foreach = array(‘from‘, ‘item‘, ‘key‘); private $tag_include = array(‘file‘);//目前只支持读取模板默认路径 public function __construct($conf) { $this->conf = &$conf; $this->template_c = $this->conf[‘template_config‘][‘template_c‘];//编译目录 $this->_tpl_suffix = $this->tpl_suffix(); } private function str_replace($search, $replace, $content) { if(empty($search) || empty($replace) || empty($content)) return false; return str_replace($search, $replace, $content); } /** * preg_match_all * @param $pattern 正则 * @param $content 内容 * @return array */ private function preg_match_all($pattern, $content) { if(empty($pattern) || empty($content)) core::show_error(‘查找模板标签失败!‘); preg_match_all(“/”.$this->template_tag_left.$pattern.$this->template_tag_right.“/is”, $content, $match); return $match; } /** * 模板文件后缀 */ public function tpl_suffix() { $tpl_suffix = empty($this->conf[‘template_config‘][‘template_suffix‘]) ? $this->tpl_suffix : $this->conf[‘template_config‘][‘template_suffix‘] ; return $tpl_suffix; } /** * 此处不解释了 * @return */ public function assign($key, $value) { $this->vars[$key] = $value; } /** * 渲染页面 * @param * 使用方法 1 * $this->view->display(‘error‘, ‘comm/‘); * 默认是指向TPL模版的跟目录,所以comm/就是 tpl/comm/error.html * 使用方法 2 * $this->view->display(‘errorfile‘); * 默认指向控制器固定的文件夹 * 例如你的域名是 heartphp/admin/index, 那么正确路径就是tpl/admin/index/errorfile.html * @return */ public function display($filename = ‘‘, $view_path = ‘‘) { $tpl_path_arr = $this->get_tpl($filename, $view_path);//获取TPL完整路径 并且向指针传送路径以及名称 if(!$tpl_path_arr) core::show_error($filename.$this->_tpl_suffix.‘模板不存在‘); //编译开始 $this->view_path_param = $view_path;//用户传递过来的模版跟目录 $this->compile(); } /** * 编译控制器 * @param * @return */ private function compile() { $filepath = $this->template_path.$this->template_name; $compile_dirpath = $this->check_temp_compile(); $vars_template_c_name = str_replace($this->_tpl_suffix, ‘‘, $this->template_name); $include_file = $this->template_replace($this->read_file($filepath), $compile_dirpath, $vars_template_c_name);//解析 if($include_file) { $this->read_config() && $config = $this->read_config(); extract($this->vars, EXTR_SKIP); [url=home.php?mod=space&uid=48608]@include[/url] $include_file; } } /** * 读取当前项目配置文件 */ protected function read_config() { if(file_exists(SYSTEM_PATH.‘conf/config.php‘)) { @include SYSTEM_PATH.‘conf/config.php‘; return $config; } return false; } /** * 解析模板语法 * @param $str 内容 * @param $compile_dirpath 模版编译目录 * @param $vars_template_c_name 模版编译文件名 * @return 编译过的PHP模板文件名 */ private function template_replace($str, $compile_dirpath, $vars_template_c_name) { if(empty($str)) core::show_error(‘模板内容为空!‘); //处理编译头部 $compile_path = $compile_dirpath.$vars_template_c_name.$this->tpl_compile_suffix;//编译文件 if(is_file($compile_path)) { //$header_content = $this->get_compile_header($compile_path); //$compile_date = $this->get_compile_header_comment($header_content); $tpl_filemtime = filemtime($this->template_path.$this->template_name); $compile_filemtime = filemtime($compile_path); //echo $tpl_filemtime.‘==‘.date(‘Y-m-d H:i:s‘, $tpl_filemtime).‘

‘; //echo $compile_filemtime.‘==‘.date(‘Y-m-d H:i:s‘, $compile_filemtime); //如果文件过期编译 当模板标签有include并且有修改时 也重新编译 //<{include file=“public/left.html”}>当修改include里的文件,非DEBUG模式时 如果不更改主文件 目前是不重新编译include里的文件,我在考虑是否也要更改,没想好,暂时这样,所以在开发阶段一定要开启DEBUG=1模式 要不然修改include文件无效 。 有点罗嗦,不知道表述清楚没 if($tpl_filemtime >$compile_filemtime || DEBUG) { $ret_file = $this->compile_file($vars_template_c_name, $str, $compile_dirpath); } else { $ret_file = $compile_path; } } else {//编译文件不存在 创建他 $ret_file = $this->compile_file($vars_template_c_name, $str, $compile_dirpath); } return $ret_file; } /** * 模板文件主体 * @param string $str 内容 * @return html */ private function body_content($str) { //解析 $str = $this->parse($str); $header_comment = “Create On##”.time().“|Compiled from##”.$this->template_path.$this->template_name; $content = “<? if(!defined(‘IS_HEARTPHP‘)) exit(‘Access Denied‘);/*{$header_comment}*/?>\r\n$str”; return $content; } /** * 开始解析相关模板标签 * @param $content 模板内容 */ private function parse($content) { //foreach $content = $this->parse_foreach($content); //include $content = $this->parse_include($content); //if $content = $this->parse_if($content); //elseif $content = $this->parse_elseif($content); //模板标签公用部分 $content = $this->parse_comm($content); //转为PHP代码 $content = $this->parse_php($content); return $content; } /** * echo 如果默认直接<{$config[‘domain‘]}>转成 <?php echo $config[‘domain‘]?> */ private function parse_echo($content) { } /** * 转换为PHP * @param $content html 模板内容 * @return html 替换好的HTML */ private function parse_php($content){ if(empty($content)) return false; $content = preg_replace(“/”.$this->template_tag_left.“(.+?)”.$this->template_tag_right.“/is”, “<?php $1 ?>”, $content); return $content; } /** * if判断语句 * <{if empty($zhang)}> * zhang * <{elseif empty($liang)}> * liang * <{else}> * zhangliang * <{/if}> */ private function parse_if($content) { if(empty($content)) return false; //preg_match_all(“/”.$this->template_tag_left.“if\s+(.*?)”.$this->template_tag_right.“/is”, $content, $match); $match = $this->preg_match_all(“if\s+(.*?)”, $content); if(!isset($match[1]) || !is_array($match[1])) return $content; foreach($match[1] as $k =>$v) { //$s = preg_split(“/\s+/is”, $v); //$s = array_filter($s); $content = str_replace($match[0][$k], “<?php if({$v}) { ?>”, $content); } return $content; } private function parse_elseif($content) { if(empty($content)) return false; //preg_match_all(“/”.$this->template_tag_left.“elseif\s+(.*?)”.$this->template_tag_right.“/is”, $content, $match); $match = $this->preg_match_all(“elseif\s+(.*?)”, $content); if(!isset($match[1]) || !is_array($match[1])) return $content; foreach($match[1] as $k =>$v) { //$s = preg_split(“/\s+/is”, $v); //$s = array_filter($s); $content = str_replace($match[0][$k], “<?php } elseif ({$v}) { ?>”, $content); } return $content; } /** * 解析 include include标签不是实时更新的 当主体文件更新的时候 才更新标签内容,所以想include生效 请修改一下主体文件 * 记录一下 有时间开发一个当DEBUG模式的时候 每次执行删除模版编译文件 * 使用方法 <{include file=“www.phpddt.com”}> * @param $content 模板内容 * @return html */ private function parse_include($content) { if(empty($content)) return false; //preg_match_all(“/”.$this->template_tag_left.“include\s+(.*?)”.$this->template_tag_right.“/is”, $content, $match); $match = $this->preg_match_all(“include\s+(.*?)”, $content); if(!isset($match[1]) || !is_array($match[1])) return $content; foreach($match[1] as $match_key =>$match_value) { $a = preg_split(“/\s+/is”, $match_value); $new_tag = array(); //分析元素 foreach($a as $t) { $b = explode(‘=‘, $t); if(in_array($b[0], $this->tag_include)) { if(!empty($b[1])) {$new_tag[$b[0]] = str_replace(“\”“, ”“, $b[1]); } else {core::show_error(‘模板路径不存在!‘); } } } extract($new_tag); //查询模板文件 foreach($this->conf[‘view_path‘] as $v){ $conf_view_tpl = $v.$file;//include 模板文件 if(is_file($conf_view_tpl)) { $c = $this->read_file($conf_view_tpl); $inc_file = str_replace($this->_tpl_suffix, ‘‘, basename($file)); $this->view_path_param = dirname($file).‘/‘; $compile_dirpath = $this->check_temp_compile(); $include_file = $this->template_replace($c, $compile_dirpath, $inc_file);//解析 break; } else { core::show_error(‘模板文件不存在,请仔细检查 文件:‘. $conf_view_tpl); } } $content = str_replace($match[0][$match_key], ‘<?php include(”‘.$include_file.‘“)?>‘, $content); } return $content; } /** * 解析 foreach * 使用方法 <{foreach from=$lists item=value key=kk}> * @param $content 模板内容 * @return html 解析后的内容 */ private function parse_foreach($content) { if(empty($content)) return false; //preg_match_all(”/“.$this->template_tag_left.”foreach\s+(.*?)“.$this->template_tag_right.”/is“, $content, $match); $match = $this->preg_match_all(”foreach\s+(.*?)“, $content); if(!isset($match[1]) || !is_array($match[1])) return $content; foreach($match[1] as $match_key =>$value) { $split = preg_split(”/\s+/is“, $value); $split = array_filter($split); $new_tag = array(); foreach($split as $v) { $a = explode(”=“, $v); if(in_array($a[0], $this->tag_foreach)) {//此处过滤标签 不存在过滤 $new_tag[$a[0]] = $a[1]; } } $key = ‘‘; extract($new_tag); $key = ($key) ? ‘$‘.$key.‘ =>‘ : ‘‘ ; $s = ‘<?php foreach(‘.$from.‘ as ‘.$key.‘ $‘.$item.‘) { ?>‘; $content = $this->str_replace($match[0][$match_key], $s, $content); } return $content; } /** * 匹配结束 字符串 */ private function parse_comm($content) { $search = array( ”/“.$this->template_tag_left.”\/foreach“.$this->template_tag_right.”/is“, ”/“.$this->template_tag_left.”\/if“.$this->template_tag_right.”/is“, ”/“.$this->template_tag_left.”else“.$this->template_tag_right.”/is“, ); $replace = array( ”<?php } ?>“, ”<?php } ?>“, ”<?php } else { ?>“ ); $content = preg_replace($search, $replace, $content); return $content; } /** * 检查编译目录 如果没有创建 则递归创建目录 * @param string $path 文件完整路径 * @return 模板内容 */ private function check_temp_compile() { //$paht = $this->template_c. $tpl_path = ($this->view_path_param) ? $this->view_path_param : $this->get_tpl_path() ; $all_tpl_apth = $this->template_c.$tpl_path; if(!is_dir($all_tpl_apth)) { $this->create_dir($tpl_path); } return $all_tpl_apth; } /** * 读文件 * @param string $path 文件完整路径 * @return 模板内容 */ private function read_file($path) { //$this->check_file_limits($path, ‘r‘); if(($r = @fopen($path, ‘r‘)) === false) { core::show_error(‘模版文件没有读取或执行权限,请检查!‘); } $content = fread($r, filesize($path)); fclose($r); return $content; } /** * 写文件 * @param string $filename 文件名 * @param string $content 模板内容 * @return 文件名 */ private function compile_file($filename, $content, $dir) { if(empty($filename)) core::show_error(”{$filename} Creation failed“); $content = $this->body_content($content);//对文件内容操作 //echo ‘开始编译了=====‘; $f = $dir.$filename.$this->tpl_compile_suffix; //$this->check_file_limits($f, ‘w‘); if(($fp = @fopen($f, ‘wb‘)) === false) { core::show_error($f.‘

编译文件失败,请检查文件权限.‘); } //开启flock flock($fp, LOCK_EX + LOCK_NB); fwrite($fp, $content, strlen($content)); flock($fp, LOCK_UN + LOCK_NB); fclose($fp); return $f; } /** * 这个检查文件权限函数 暂时废弃了 * @param [$path] [路径] * @param [status] [w=write, r=read] */ public function check_file_limits($path , $status = ‘rw‘) { clearstatcache(); if(!is_writable($path) && $status == ‘w‘) { core::show_error(”{$path}

没有写入权限,请检查.“); } elseif(!is_readable($path) && $status == ‘r‘) { core::show_error(”{$path}

没有读取权限,请检查.“); } elseif($status == ‘rw‘) {//check wirte and read if(!is_writable($path) || !is_readable($path)) { core::show_error(”{$path}

没有写入或读取权限,请检查“); } } } /** * 读取编译后模板的第一行 并分析成数组 * @param string $filepath 文件路径 * @param number $line 行数 * @return 返回指定行数的字符串 */ /* private function get_compile_header($filepath, $line = 0) { if(($file_arr = @file($filepath)) === false) { core::show_error($filepath.‘

读取文件失败,请检查文件权限!‘); } return $file_arr[0]; } */ /** * 分析头部注释的日期 * @param string $cotnent 编译文件头部第一行 * @return 返回上一次日期 */ /* private function get_compile_header_comment($content) { preg_match(”/\/\*(.*?)\*\//", $content, $match); if(!isset($match[1]) || empty($match[1])) core::show_error(‘编译错误!‘); $arr = explode(‘|‘, $match[1]); $arr_date = explode(‘##‘, $arr[0]); return $arr_date[1]; } */ /** * 获取模板完整路径 并返回已存在文件 * @param string $filename 文件名 * @param string $view_path 模板路径 * @return */ private function get_tpl($filename, $view_path) { empty($filename) && $filename = $this->tpl_name; //遍历模板路径 foreach($this->conf[‘view_path‘] as $path) { if($view_path) {//直接从tpl跟目录找文件 $tpl_path = $path.$view_path; $view_file_path = $tpl_path.$filename.$this->_tpl_suffix; } else {//根据目录,控制器,方法开始找文件 $view_file_path = ($tpl_path = $this->get_tpl_path($path)) ? $tpl_path.$filename.$this->_tpl_suffix : exit(0); } if(is_file($view_file_path)) { //向指针传送模板路径和模板名称 $this->template_path = $tpl_path;// $this->template_name = $filename.$this->_tpl_suffix; return true; } else { core::show_error($filename.$this->_tpl_suffix.‘模板不存在‘); } } } /** * 获取模板路径 * @param string $path 主目录 * @return URL D和M的拼接路径 */ private function get_tpl_path($path = ‘‘) { core::get_directory_name() && $path_arr[0] = core::get_directory_name(); core::get_controller_name() && $path_arr[1] = core::get_controller_name(); (is_array($path_arr)) ? $newpath = implode(‘/‘, $path_arr) : core::show_error(‘获取模板路径失败!‘) ; return $path.$newpath.‘/‘; } /** * 创建目录 * @param string $path 目录 * @return */ private function create_dir($path, $mode = 0777){ if(is_dir($path)) return false; $dir_arr = explode(‘/‘, $path); $dir_arr = array_filter($dir_arr); $allpath = ‘‘; $newdir = $this->template_c; foreach($dir_arr as $dir) { $allpath = $newdir.‘/‘.$dir; if(!is_dir($allpath)) { $newdir = $allpath; if(!@mkdir($allpath, $mode)) { core::show_error( $allpath.‘

创建目录失败,请检查是否有可都写权限!‘); } chmod($allpath, $mode); } else { $newdir = $allpath; } } return true; } public function __destruct(){ $this->vars = null; $this->view_path_param = null; }}

希望本文所述对大家的php程序设计有所帮助,

篇2:雅思听力比较类题型实例解析

例1 剑7 Test 2 Q 34

A German study showed there was greater ‘mixed handedness’ in musicians who

A started playing instruments in early youth

B play a string instrument such as the violin

C practice a great deal on their instrument

思路透析:题干中包括greater这个明显的比较级,要提醒考生们注意的是,在实际的听力中,比较级的用词可以发生同义转换,但是这种比较关系的表达一定在听力文本中是存在的,所以要特别关注在听力内容中比较的表达方式,答案就在其中。这与填空题是一致的,在填空题中如若发现比较表达方式,那我们可以把它当特殊关键词来对待,耐心等待听力素材中的比较表达后即可得到答案。

实际做题:在实际做题中,当听到Germany study 时确定做题位置。

原文:keyboard players had high levels of mixed handedness, whereas string players like violinists strongly favored one hand. Also those who started younger were more mixed handed.

解题:从原文中我们不难看出,小提琴演奏者的用手习惯比较倾向于某一侧。所以排除B。紧接着出现的整句话里出现了两处比较级,more mixed handed 与greater mixed handedness 形成同义转换,younger 与选项A中的early youth 对应,所以正确答案为A。

例2 剑7 Test 1 Q 12

The company has most camping sites in

A France

B Italy

C Switzerland

思路透析:题干中包含有most sites 这个最高级的表达方式,故做好准备去听比较关系同义转换即可得到答案。

实际做题:当听到300 sites 以及Italy 等信息后可以定位做题位置

原文:In Italy we now have some 64 sites that we either own, or have exclusive use of . France is where we have the majority of sites, and we currently have a project to expand into Switzerland.

解题:不难发现majority 和most 之间存在同义转换,故答案为A。

例3 剑2 Test 3 Q 31

The driest continent is

A Australia

B Africa

C Antarctica

思路透析:题干中有driest 这个最高级,要高度注意比较关系的同义转换。

实际做题:听到Australia 后确定做题位置。

原文:As I have said, Australia is a dry continent, second only to Antarctica in its lack of rainfall.

解题:不难发现答案是C,但值得我们注意的是,second only to 是一种特殊的比较表达方式。

篇3:雅思听力比较类题型实例解析

例4 剑3 Test 1 Q 32

According to the speaker, the main cause of back pain in women is

A pregnancy

B osteoporosis

C lack of exercise

思路透析:在审题中发现有main cause 这样的间接比较提示词,那此题很显然要列出几个造成女性背疼的原因,但是要进行对比得出谁是主要原因。

实际做题:听到women 就确定了做题位置。

原文:The majority of our patients at the clinic tend to be women. They are especially vulnerable because of pregnancy but also because of osteoprosis, which I personally believe to be the major cause of problems for women.

解题:在原文中我们不难发现pregnancy 和 osteoprosis 是并列出现的,但是后者紧跟着就是一个非限定性的定语从句,指出后者是major cause,也就是main cause的同义转换,故答案为B。

例5 剑2 Test 4 Q 32

The main research method was

A interviews

B questionnaires

C observation

思路透析:审题时发现有main 这样一个比较关系提示词,做好听到比较关系的准备。

雅思听力语法练习:单词与短语

Part I :单词与短语

pub n.酒吧;leave v.离开;chair n.椅子;landlord n.房东;bill n.帐单;in a few minutes几分钟后;return v.回来;give sth. back to sb.将某物还给某人

Part II:语法学习

过去完成时、状语从句、介词短语作状语的综合运用示例

示例1:After I had had lunch at a village pub, I looked for my bag.

示例2:I had left it on a chair beside the door.

示例3:As I was looking for it, the landlord came in.

示例4:In a few minutes, he returned with my bag and gave it back to me.

示例5:My dog had taken it into the garden.

Part III:综合训练

(1) I had had lunch at a village pub, I (2 look) for my bag. I (3) left it (4) a chair beside the door,(5)now it wasn‘t there. (6) I was looking for it, the landlord came in. (7) he asked me (8) I had had a good meal,I answered that I had had a very good meal,(9) (10) I couldn’t pay the bill,(11) I didn‘t have my bag. The landlord smiled (12) immediately went out (13) (14) a few minutes,he returned (15) my bag. (16 say) 'Sorry' to me, he gave it back (17) me. He said (18) his dog (19) taken it into the garden (20) (21) it often (22) that.

答案:

(1) After;(2) looked;(3) had;(4) on;(5) but;(6) As;(7) When;(8) if;(9) but;(10) that;(11) for//as;(12) and;(13) and;(14) in;(15) with;(16) Saying;(17) to;(18) that;(19) had;(20) and;(21) that;(22) did

Part IV:句型转换

将下面句子进行最大程度的合并:

1. I had had lunch at a village pub. I looked for my bag.

2. I had left it on a chair beside the door. Now it wasn‘t there.

3. I was looking for it. The landlord came in.

4. He asked me if I had had a good meal. I answered ‘Yes’。

5. I had had a very good meal. I couldn‘t pay the bill. I didn’t have my bag.

6. The landlord smiled. He immediately went out. In a few minutes, he returned with my bag.

7. He said ‘Sorry’ to me. He gave it back to me.

8. He said that his dog had taken it into the garden. He also said that it often did that.

雅思听力选择题的万能解题方法介绍

很多考生都认为选择题比较容易,因为就算完全不懂,还有20%-25%的机会答中(视乎有四个还是五个选择)。雅思听力选择题通常都会用两个对话,讲课或演讲。平日可用英语讲课或演讲的录音带来练习。

雅思听力选择题答案大致可分为三类:

1、被直接或间接地支持(Directly or indirectly supported)

2、直接或间接地矛盾(Directly or indirectly contradicted)

3、并未提及(Not mentioned)

在选择答案时,应先找出不正确的答案:

1、通常在几个答案之中,至少有一个答案是明显地不正确

2、有些答案也许是直接有矛盾 - 即明显地与原意有分别或间接有矛盾 - 即从部分内容结论出这个选择是不正确的或并非全对 - 差不多(但并不是完全)和读者的意思一样

3、有时候其中一些选择是从来未提及过的然后,找出正确答案:

1、被直接支持 - 有时候正确答案能直接地从聆听内容中找出来

2、被间接支持 - 有时候正确答案是由内容的意思引导出来的结论

注意,应考虑所有的选择,不要看到一个认为正确选择的时候忽略了其他选择,有时候最后的选择是“all of the above”或“none of the above”,如您不看清楚,可能只选择了一个“部分正确”的答案。

另外,很多时候正确的答案会是最长的那个选择,因此,当排除所有没有可能的答案而仍未能找到正确答案时,可考虑较长的那个。

雅思听力发现段落主题的技巧介绍

雅思听力和阅读需要许多技巧。发现某段落的主题就是其中之一。

什么是段落主题?怎样才能找到它?

段落主题也可理解为这个段落的中心意思,或者是体现这个段落的主旨和方向的核心意思。换句话说就是,你找主题的过程也就是确定这个段落的主要目的的过程:它是要告诉你一件事?还是解释说明,亦或是详细叙述?它是要与其它某事或某物作比较,还是要反驳某件事,亦或是就某点说服你?凡此种.种目的,不一而足。明白了这一点,你就能够比较容易地找到段落的中心意思了。

体现段落中心意思的句子叫主题句,常放在段首,而且往往是段落的第一句。其后所跟的其它句子,提供支持这个主题句的全部细节。

当要讨论的是一个比较费解的意思,或者说当一个段落的目的是要说服你时,主题句有时放在段末。

如果某个段落有着暗含的中心意思,通常没有明确的主题句来体现。这个暗含的中心意思要从这个段落的整体来把握。

【PHP解析类实例】相关文章:

1.毕业生求职陷阱实例解析

2.GRE填空高难题型实例解析

3.实例解析托福作文如何突出重点

4.php简历

5.php 面试题

6.PHP编程习惯

7.php程序员述职报告

8.大学生PHP程序员个人简历

9.php程序员面试自我介绍

10.PHP程序员笔试题

下载word文档
《PHP解析类实例.doc》
将本文的Word文档下载到电脑,方便收藏和打印
推荐度: 评级1星 评级2星 评级3星 评级4星 评级5星
点击下载文档

文档为doc格式

  • 返回顶部