substr()函数
substr(string,start,length)
string表示要截取的对象,start表示从哪个位置开始截取,0表示从头开始,正数表示从这个数的位置后面截取,负数表示从结尾算开始截取的位置,但依然是从左到右截,length表示截取长度.负数表示排除或忽略结尾多少个字符.比如:
代码如下 | 复制代码 |
<?php $siteurl = 'www.111cn.net'; print_r (substr($siteurl,4));exit; |
//则返回: 111cn.net表示从头第4个字符开始,返回后面的所有字符.
代码如下 | 复制代码 |
<?php |
如果要截取双字节的汉字.则要用PHP mb_substr函数或mb_strcut函数,但这两个函数依赖php扩展php_mbstring.dll组件,所以要配置你的服务器.即把php安装目录中的php_mbstring.dll文件复制到你的windows 2003 的c盘的Windows/system32目录中.
举个例子:
代码如下 | 复制代码 |
<?php |
输出:这样一来我的字
代码如下 | 复制代码 |
<?php |
输出:这样一
从上面的例子可以看出,mb_substr是按字来切分字符,而mb_strcut是按字节来切分字符,但是都不会产生半个字符的现象……
代码如下 | 复制代码 |
<?php echo mb_substr('飞花院博客feihuayuan',0,9,'utf-8'); mb_strcut('飞花院博客feihuayuan',0,9,'utf-8'); |
再举个例子,有一段文字, 分别用mb_substr和mb_strcut来做切分:
PLAIN TEXT
CODE:
代码如下 | 复制代码 |
<?php echo "mb_substr:" . mb_substr($str, 0, 6, 'utf-8'); echo "<br>"; echo "mb_strcut:" . mb_strcut($str, 0, 6, 'utf-8'); |
输出结果如下:
mb_substr:我是一串比较
mb_strcut:我是
本文章总结
从上面实例可以看得出来,substr只支持单字节的,这样只适合英文截取,而mb_substr函数是双字体截取了,正好可用于中文了,而mb_strcut是三字节了。
本文章来给大家介绍我们在提交表单时如果表单使用的是get方式,然后我们利用get获取到的会是乱码,下面我来看看是如何解决此问题的。本来打算这样使用
代码如下 | 复制代码 |
<a href="list.php?plate=辖区动态" charset="utf-8" target="main">[查看辖区动态]</a> |
结果在list.php页面得到的是—–查看[辖区动怿]
我想可能是汉字的“态"这个编码和某些东西冲突了,所以。。。
我找到了网上这样解决:
使用:
代码如下 | 复制代码 |
<a href="list.php?plate=<?php echo urlencode(“辖区动态");?>" charset="utf-8" target="main">[查看]</a> |
然后在list.php页面这样用
代码如下 | 复制代码 |
$plate=urldecode($_GET['plate']); |
也就不会乱码和传递不正常了
关于string urlencode ( string $str )函数
此函数便于将字符串编码并将其用于 URL 的请求部分,同时它还便于将变量传递给下一页。
Example #1 urlencode() 例子
代码如下 | 复制代码 |
<?php |
Example #2 urlencode() 与 htmlentities() 例子
代码如下 | 复制代码 |
<?php |
基本信息
E_NOTICE 表示一般情形不记录,只有程序有错误情形时才用到,例如企图存取一个不存在的变量,或是呼叫 stat() 函数检视不存在的文件。
E_WARNING 通常都会显示出来,但不会中断程序的执行。这对除错很有效。例如:用有问题的正则表达式呼叫 ereg()。
E_ERROR 通常会显示出来,亦会中断程序执行。意即用这个遮罩无法追查到内存配置或其它的错误。
E_PARSE 从语法中解析错误。
E_CORE_ERROR 类似 E_ERROR,但不包括 PHP 核心造成的错误。
E_CORE_WARNING 类似 E_WARNING,但不包括 PHP 核心错误警告。
例子:
error_reporting = E_ALL & ~E_NOTICE ; 显示所有的错误,除了提醒
error_reporting = E_COMPILE_ERROR|E_ERROR|E_CORE_ERROR ; 仅显示错
error_reporting=E_ERROR :只会报告致命性错误
基本上设置
error_reporting = E_ALL & ~E_NOTICE ; 显示所有的错误,除了提醒
例子
代码如下 | 复制代码 |
|
整数。表示旧的PHP的错误级别。(Returns the old error_reporting level.)
手册上的例子:
Value | Constant | Description | Note |
---|---|---|---|
1 | E_ERROR (integer) | Fatal run-time errors. These indicate errors that can not be recovered from, such as a memory allocation problem. Execution of the script is halted. | |
2 | E_WARNING (integer) | Run-time warnings (non-fatal errors). Execution of the script is not halted. | |
4 | E_PARSE (integer) | Compile-time parse errors. Parse errors should only be generated by the parser. | |
8 | E_NOTICE (integer) | Run-time notices. Indicate that the script encountered something that could indicate an error, but could also happen in the normal course of running a script. | |
16 | E_CORE_ERROR (integer) | Fatal errors that occur during PHP’s initial startup. This is like an E_ERROR, except it is generated by the core of PHP. | since PHP 4 |
32 | E_CORE_WARNING (integer) | Warnings (non-fatal errors) that occur during PHP’s initial startup. This is like an E_WARNING, except it is generated by the core of PHP. | since PHP 4 |
64 | E_COMPILE_ERROR (integer) | Fatal compile-time errors. This is like an E_ERROR, except it is generated by the Zend Scripting Engine. | since PHP 4 |
128 | E_COMPILE_WARNING (integer) | Compile-time warnings (non-fatal errors). This is like an E_WARNING, except it is generated by the Zend Scripting Engine. | since PHP 4 |
256 | E_USER_ERROR (integer) | User-generated error message. This is like an E_ERROR, except it is generated in PHP code by using the PHP function trigger_error(). | since PHP 4 |
512 | E_USER_WARNING (integer) | User-generated warning message. This is like an E_WARNING, except it is generated in PHP code by using the PHP function trigger_error(). | since PHP 4 |
1024 | E_USER_NOTICE (integer) | User-generated notice message. This is like an E_NOTICE, except it is generated in PHP code by using the PHP function trigger_error(). | since PHP 4 |
2048 | E_STRICT (integer) | Run-time notices. Enable to have PHP suggest changes to your code which will ensure the best interoperability and forward compatibility of your code. | since PHP 5 |
4096 | E_RECOVERABLE_ERROR (integer) | Catchable fatal error. It indicates that a probably dangerous error occured, but did not leave the Engine in an unstable state. If the error is not caught by a user defined handle (see also set_error_handler()), the application aborts as it was an E_ERROR. | since PHP 5.2.0 |
8191 | E_ALL (integer) | All errors and warnings, as supported, except of level E_STRICT in PHP < 6. | 6143 in PHP 5.2.x and 2047 previously |
parse_url函数
我们先来了解一下parse_url函数,官方解决
说明
mixed parse_url ( string $url [, int $component = -1 ] )
本函数解析一个 URL 并返回一个关联数组,包含在 URL 中出现的各种组成部分。
本函数不是用来验证给定 URL 的合法性的,只是将其分解为下面列出的部分。不完整的 URL 也被接受, parse_url() 会尝试尽量正确地将其解析。
要解析的 URL。无效字符将使用 _ 来替换。
实例
代码如下 | 复制代码 |
$url = "http://www.111cn.net.net/welcome/"; $parts = parse_url($url); print_r($parts);
|
也可以自己去写一个算法!如下
代码如下 | 复制代码 |
|
调用方法
代码如下 | 复制代码 |
$arr = getParams(); |
结果
结果: Array ( [_p] => index [_a] => show [x] => 12 [y] => 23 )
在php中判断是否为纯字母我们可直接使用正则/^[a-zA-Z]$/来验证了,包括大小写字母哦,有需要了解的同学可参考参考。上代码
代码如下 | 复制代码 |
<?php header('Content-type: text/html; charset=utf-8'); $str = "dasdadsfsadASDSADS"; if (preg_match('/^[a-zA-Z]+$/',$str)) ?> |
这个就是代码
代码如下 | 复制代码 |
preg_match('/^[a-zA-Z]+$/',$str) |
如果是字母则返回TRUE,否则返回FALSE
其它的判断
代码如下 | 复制代码 |
if(preg_match("/^d*$/", "4312")) if(preg_match("/^[a-z]*$/i", "fdsFDfd")) if(preg_match("/^[a-zd]*$/i", "fd4fd34")) |