Home > Backend Development > PHP Tutorial > PHP判断字符是否为字母[非正则表达式判断]

PHP判断字符是否为字母[非正则表达式判断]

WBOY
Release: 2016-06-23 13:27:26
Original
2989 people have browsed it

一直都是用正则表达式来判断,个人不喜欢这种方式,想到ASCII码可以来区别。


找到php有个ord()函数:W3C文档:http://www.w3school.com.cn/php/func_string_ord.asp


ord() 函数返回字符串的首个字符的 ASCII 值。

大小写字母的ASCII码在

65-90,97-122
Copy after login

之间。

<?php$str="http://my.oschina.net/rain21/admin/new-blog";$arr=str_split($str);/*65-90,97-122*/foreach($arr as $k=>$v){	$check=ord($v);	if(($check>=65&&$check<=90)||($check>=97&&$check<=122)){		$newArr[]=empty(rand(0,1))?strtoupper($v):$v;	}else{		$newArr[]=$v;	}}echo implode('',$newArr);
Copy after login




source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template