php中parse_url()函数用法介绍
PHP中一个好用的函数parse_url,特别方便用来做信息抓取的分析,举例子如下:
代码如下 | 复制代码 |
$url = "http://www.111cn.net/aaaa/";$parts = parse_url($url); 输出: |
又如:
代码如下 | 复制代码 |
<?php$url = 'http://username:password@hostname/path?arg=value#anchor';print_r(parse_url($url));echo parse_url($url, PHP_URL_PATH);?> 输出: |
可以看到,可以很容易分解出一个URL的各个部,那如果要拿指定的部分出来的话也很容易,如
echo parse_url($url, PHP_URL_PATH);
就是在第二个参数中,设定如下的参数:
PHP_URL_SCHEME, PHP_URL_HOST, PHP_URL_PORT, PHP_URL_USER, PHP_URL_PASS, PHP_URL_PATH, PHP_URL_QUERY or PHP_URL_FRAGMENT.
本文章来讲述一下关于mvc中controller类教程,通过上两节我们知道 程序通过单一入口文件的route类决定了 唯一的moudle, conttoller, action,并在最后执行了
代码如下 | 复制代码 |
$route->run(); /** * 执行相应的 MCA * */ private function run () { $filePath = APPLICATION_PATH.'/controller/'.$this->_moudle.'/'.$this->_conttoller.'.inc.php'; $isNo = 0; if(file_exists($filePath)) { include "$filePath"; $controller_tp = $this->_conttoller.'Controller'; $controller = new $controller_tp;
if (method_exists($controller,$this->_action.'Action')) { $acion_tmp = $this->_action.'Action'; $controller->$acion_tmp(); }else { $isNo = 1; }
}else { $isNo = 1; }
if ($isNo) { $filePath = APPLICATION_PATH.'/controller/default/index.inc.php'; $this->_moudle = $this->_default['module']; $this->_conttoller = $this->_default['conttoller']; $this->_action = $this->_default['action'];
($this->_moudle != $this->_default['module']) && include "$filePath"; $controller = new indexController; $controller->indexAction(); } }
|
当相关'Controller'文件存在时执行
代码如下 | 复制代码 |
include "$filePath"; $controller_tp = $this->_conttoller.'Controller'; $controller = new $controller_tp; |
上述三行代码的意思是,根据确定好的 conttoller 包含相应文件,并实例化相应的conttoller。
代码如下 | 复制代码 |
$acion_tmp = $this->_action.'Action'; $controller->$acion_tmp(); |
根据相应的Action 执行相应的action
所有的 Controller 类都集成一个公用的Controller 类,本节课我们就来分析一下公共的Controller 类
<?php
/**
* 前台公共类 接口
* 实现公共部分代码
*/
/**
* 本文件只能被index。php包含
*/
defined("WEB_AUTH") || die("NO_AUTH");
/**
* 包含菜单配置文件
*/
代码如下 | 复制代码 |
class Controller { public $tpl; public $controller; public $body;//右边菜单 public $_route ; public $html_; public $tpl_;
/* * 构造函数 */ public function __construct() { $this->init(); }
/* * 初始化变量,顶部菜单和模板 */ protected function init() { global $TPL,$route; $this->tpl = $TPL; $this->_route = $route; }
/** * 模板变量传第 */ protected function diplayTpl() { $this->body || $this->body = $this->_route->getActionName(); $this->tpl->assign("body",$this->body); /*设置本控制器的模板目录*/ $this->controller ||$this->controller =$this->_route->getControllerName(); $this->tpl->assign("controller",$this->controller); $this->tpl->display($this->layout); } /** * smarty封装类 * @param string $name * @param string $value */ public function assign($name,$value) { $this->tpl->assign($name,$value); }
/** * 显示另外的模板 * @param string $name * @param string $value */ protected function displayOther($file) { $this->assign("otherTpl",TRUE); $this->tpl->display($file); } /** * 显示某个MCA的body模板 * 0=>m 1=>c =>a */ protected function getMcaBody($array) { return 'http://www.cnblogs.com/../'.$array[0].'/body/'.$array[1].'/'.$array[2]; } /* * 析构函数,显示页面 */ protected function __destruct() { $this->tpl->_tpl_vars['otherTpl'] || $this->diplayTpl(); } /** * 中途退出 */ protected function _exit($msg = "") { $this->assign("otherTpl",TRUE); die($msg); }
/** * 用 $this->html_var=value放法给变量赋值 * 用 $this->tpl_var=value放法给变量赋值 */ protected function __set($name,$value) { if(strtolower(substr($name,0,5)) == "html_" || strtolower(substr($name,0,4)) == "tpl_") { $this->assign(substr($name,5),$value); } } } ?> |
首先看
代码如下 | 复制代码 |
protected function __destruct() { $this->tpl->_tpl_vars['otherTpl'] || $this->diplayTpl(); } |
这是所有Controller 类 生命周期结束时候要执行的函数(搜索一下php魔术方法 查看详情)
本框架利用这时候解析模板,这样的好处是,当Controller中相关执行完相关数据处理,后自动执行相关的模板(View);而不用每次在程序最后调用模板
代码如下 | 复制代码 |
protected function __set($name,$value) { if(strtolower(substr($name,0,5)) == "html_" || strtolower(substr($name,0,4)) == "tpl_") { $this->assign(substr($name,5),$value); } } |
这个函数简化了程序向模板传递变量的方法,以smarty为例,在程序中需要执行 $tpl->assign(‘key’,$value);
来向模板中注册变量,而此函数中简化了此方法 ,只需 $this->html_key=$value;来实现相同的作用.(利用开发环境的提示功能,在前面声明
代码如下 | 复制代码 |
public $html_; public $tpl_; |
自定义函数
7.2.1函数命名基本原则:
1.函数名不能和已有的函数名重名。
2.函数名只能包含字母、数字和下划线。
3.函数名不能以数字开头
7.2.2基本使用:用function进行声明
代码如下 | 复制代码 |
<?php //创建函数 function funcCountArea($radius) { return $radius*$radius*pi(); }
//使用函数 $area = funcCountArea(20); echo $area; echo '<br/>';
$area2 = funcCountArea(30); echo $area2; ?> output 1256.63706144
|
7.2.3按值传参
代码如下 | 复制代码 |
<?php $a = 5; function funcChange($a) { $a = 2 * $a; } funcChange ($a); echo $a; ?> output 5 |
7.2.4按引用传参
代码如下 | 复制代码 |
<?php $a = 5; function funcChange(&$a) { $a = 2 * $a; } funcChange ($a); echo $a; ?> output 10 |
7.2.5返回多个值的函数调用
代码如下 | 复制代码 |
<?php function funcUserInfo($username,$password) { $userInfo = array($username,$password); return $userInfo; }
$arr = funcUserInfo('anllin','123456');
print_r($arr); ?> output Array ( [0] => anllin [1] => 123456 ) |
7.2.6另一种返回多个值的函数调用(实用:推荐)
代码如下 | 复制代码 |
<?php function funcUserInfo($username, $password) { $userInfo [] = $username; $userInfo [] = $password; return $userInfo; }
$arr[] = funcUserInfo ( 'Bob', '512655' ); $arr[] = funcUserInfo ( 'John', '458736' ); $arr[] = funcUserInfo ( 'Mark', '925472' ); print_r ( $arr ); ?> output Array ( [0] => Array ( [0] => Bob [1] => 512655 ) [1] => Array ( [0] => John [1] => 458736 ) [2] => Array ( [0] => Mark [1] => 925472 ) ) |
注意:函数调用是不区分大小写的,但是变量名是区分大小写的。
在php中如何来理解作用域呢,下面我们来介绍一下关于局部变量:在函数内部声明的变量。全局变量: 在函数外部声明的变量,作详细的说明7.2.8局部变量转换成全局变量
代码如下 | 复制代码 |
<?php $a = 5; function funcChangeValue() { global $a; $a = 10; }
funcChangeValue();
echo $a; ?> output 10 |
7.2.9超级全局变量$GLOBALR的使用
代码如下 | 复制代码 |
<?php $GLOBALS['a'] = 5; function funcChangeValue() { $GLOBALS['a'] = 10; }
funcChangeValue();
echo $GLOBALS['a']; ?> Output 10 |
数组是一组有某种共同特性的元素,包括相似性和类型。
每个元素由一个特殊的标识符来区分,称之为key,而每个key都有一个value
1.创建数组的两种方式:
1.1 用array()函数
代码如下 | 复制代码 |
<?php output |
1.2 用range()函数
代码如下 | 复制代码 |
<?php output c o q t |
2.循环访问数组元素的两种方式:
2.1 for循环
代码如下 | 复制代码 |
<?php output |
相关文章
- eval函数在php中是一个函数并不是系统组件函数,我们在php.ini中的disable_functions是无法禁止它的,因这他不是一个php_function哦。 eval()针对php安全来说具有很...2016-11-25
- 在php中eval是一个函数并且不能直接禁用了,但eval函数又相当的危险了经常会出现一些问题了,今天我们就一起来看看eval函数对数组的操作 例子, <?php $data="array...2016-11-25
Python astype(np.float)函数使用方法解析
这篇文章主要介绍了Python astype(np.float)函数使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-06-08- 这篇文章主要介绍了Python中的imread()函数用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-16
- 本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
- using 指令有两个用途: 允许在命名空间中使用类型,以便您不必限定在该命名空间中使用的类型。 为命名空间创建别名。 using 关键字还用来创建 using 语句 定义一个范围,将在此...2020-06-25
- 下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
- CREATE FUNCTION ChangeBigSmall (@ChangeMoney money) RETURNS VarChar(100) AS BEGIN Declare @String1 char(20) Declare @String2 char...2016-11-25
- 这篇文章主要介绍了C++中Sort函数详细解析,sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变...2022-08-18
Android开发中findViewById()函数用法与简化
findViewById方法在android开发中是获取页面控件的值了,有没有发现我们一个页面控件多了会反复研究写findViewById呢,下面我们一起来看它的简化方法。 Android中Fin...2016-09-20- strstr() 函数搜索一个字符串在另一个字符串中的第一次出现。该函数返回字符串的其余部分(从匹配点)。如果未找到所搜索的字符串,则返回 false。语法:strstr(string,search)参数string,必需。规定被搜索的字符串。 参数sea...2013-10-04
PHP函数分享之curl方式取得数据、模拟登陆、POST数据
废话不多说直接上代码复制代码 代码如下:/********************** curl 系列 ***********************///直接通过curl方式取得数据(包含POST、HEADER等)/* * $url: 如果非数组,则为http;如是数组,则为https * $header:...2014-06-07- Foreach 函数(PHP4/PHP5)foreach 语法结构提供了遍历数组的简单方式。foreach 仅能够应用于数组和对象,如果尝试应用于其他数据类型的变量,或者未初始化的变量将发出错误信息。...2013-09-28
- free函数是释放之前某一次malloc函数申请的空间,而且只是释放空间,并不改变指针的值。下面我们就来详细探讨下...2020-04-25
- PHP 函数 strip_tags 提供了从字符串中去除 HTML 和 PHP 标记的功能,该函数尝试返回给定的字符串 str 去除空字符、HTML 和 PHP 标记后的结果。由于 strip_tags() 无法实际验证 HTML,不完整或者破损标签将导致更多的数...2014-05-31
- 分享一个PHP加密解密的函数,此函数实现了对部分变量值的加密的功能。 加密代码如下: /* *功能:对字符串进行加密处理 *参数一:需要加密的内容 *参数二:密钥 */ function passport_encrypt($str,$key){ //加密函数 srand(...2015-10-30
SQL Server中row_number函数的常见用法示例详解
这篇文章主要给大家介绍了关于SQL Server中row_number函数的常见用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-12-08php的mail函数发送UTF-8编码中文邮件时标题乱码的解决办法
最近遇到一个问题,就是在使用php的mail函数发送utf-8编码的中文邮件时标题出现乱码现象,而邮件正文却是正确的。最初以为是页面编码的问题,发现页面编码utf-8没有问题啊,找了半天原因,最后找到了问题所在。 1.使用 PEAR 的...2015-10-21- 下面小编就为大家带来一篇C#中加载dll并调用其函数的实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
- 这篇文章主要介绍了C#虚函数用法,实例分析了C#中虚函数的功能与基本使用技巧,需要的朋友可以参考下...2020-06-25