php地市联动分类排序程序

 更新时间:2016年11月25日 17:30  点击:2374
一个网友写的地市联动的php分类程序,大家可参考参考。

写一个函数,将数据

 代码如下 复制代码
$array = array(
    0=>array("","河北"),
    1=>array("","北京"),
    2=>array(0,"保定"),
    3=>array(1,"海淀"),
    4=>array(3,"中关村"),
    5=>array(2,"涿州")
);


处理后返回如下:
河北
-保定
--涿州
北京
-海淀
--中关村

 

 代码如下 复制代码
function typeArray($array){
   $con = null;
   foreach ($array as $k=>$v){
        $na[$k] = is_numeric($v[0]) ? $na[$v[0]].$k."|" : $k."|";
    }
   asort($na);
   foreach ($na as $k=>$v){
     $s = substr_count($v,"|");
      $con .= str_repeat("-",($s-1)).$array[$k][1]."n";
    }
    return $con;
}
__autoload() 是PHP执行环境中约定的一个函数而非某个类的方法,如果一个类在使用之前没有加载到当前文件,会自动调用 __autoload() 函数来加载该类,通常这些类的加载规则都是约定的,比如这些类包含在以类名命名的文件内,该方法可以实现类的按需加载,避免脚本执行前加载不必要的类从而降低资源占用、提交性能。

注意:__autoload() 内的错误不能被 try-catch 捕获。

 代码如下 复制代码

function __autoload($class_name){

     require_once(PATH.'/calsses/'.$class_name.'.php');

}

$obj1 = new mycalss1();

注册 __autoload() 自动调用的函数:

spl 代码库在 PHP5.0 之后默认自动启用

spl_autoload_register([callback]); //不将具体实现的加载代码写在 __autoload() 内,可使用该函数注册回调函数。

如果使用类的方法作为回调函数需要传入一个数组:

 代码如下 复制代码

spl_autoload_register(array('class_name'|$obj,'method_name'));

例如:

spl_autoload_register(array($this,'autoloadClass'));

spl_autoload_register(array('YiiBase','autoload'));// YII 框架的自动加载类的实现, YiiBase 类实现了一个autoload 方法。  spl_autoload_register() 可以注册多个加载函数,成功加载类文件之前将逐个尝试所有注册的加载函数。这在不同的类使用不同逻辑来导入类文件的时候很有用。

spl_autoload_unregister(); //取消某个注册的加载函数,参数与 spl_autoload_register() 相同.

spl_autoload_functions();// 以数组形式返回所有注册的 __autoload() 函数

 

spl_autoload(class_name[,file_extentions]); // __autoload() 函数的默认实现。 spl_autoload_register() 被调用时如果没有传入 函数名,则默认使用该函数,该函数的执行规则是: 类名转为小写作为文件名,传入的 file_extentions(多个扩展名以逗号隔开,默认为 .inc 和 .php)为扩展名,根据得到的文件名尝试在 php.ini 内设置的 include paths 中搜索。

 spl_autoload_call(class_name);//手动调用所有注册的 __autoload() 函数来主动加载类文件

spl_autoload_extentions([file_extentions]); //注册或返回 spl_autoload() 中可以使用的文件扩展名,扩展名可以是 .a.b 这样的形式,例如:

 

 代码如下 复制代码

spl_autoload_extentions(".class.php");

spl_autoload_register(); //使用spl_autoload() 来尝试自动加载类文件

//这样 spl_autoload('myclassName'); 会尝试加载 文件 "myclassName.class.php" .

实例

1、将需要注册的类放在一个数组中

 代码如下 复制代码


<?php
final class Utils {

    private function __construct() {

    }

    public static function getClasses($pre_path = '/') {
        $classes = array(
                'DBConfig' => $pre_path.'DBConfig/DBConfig.php',
                'User' => $pre_path.'Model/User.php',
                'Dao' => $pre_path.'Dao/Dao.php',
                'UserDao' => $pre_path.'Dao/UserDao.php',
                'UserMapper' => $pre_path.'Mapping/UserMapper.php',
        );
        return $classes;
    }
}
?>

2、注册数组

注意:步骤1中的类的路径都是相对于init.php而言的,不是相对于Utils而言的,这是因为我们通过init.php里的自动加载函数spl_autoload_register来require类的

 代码如下 复制代码


<?php
require_once '/Utils/Utils.php';
final class Init {
   
    /**
     * System config.
     */
    public function init() {
        // error reporting - all errors for development (ensure you have
        // display_errors = On in your php.ini file)
        error_reporting ( E_ALL | E_STRICT );
        mb_internal_encoding ( 'UTF-8' );
        //registe classes
        spl_autoload_register ( array ($this,'loadClass' ) );
    }
   
    /**
     * Class loader.
     */
    public function loadClass($name) {
        $classes = Utils::getClasses ();
        if (! array_key_exists ( $name, $classes )) {
            die ( 'Class "' . $name . '" not found.' );
        }
        require_once $classes [$name];
    }
}
$init = new Init ();
$init->init ();
?>

3、本例中在使用处test.php里require init.php

 

 代码如下 复制代码

<?php
require_once 'Init.php';

$dao = new UserDao();
$result = $dao->findByName('zcl');
?>

常用的php各种验证正则表达式程序
 代码如下 复制代码
class validator
 
{
 
    /**
 
     * Checks that a field is exactly the right length.
 
     * Constructer PHP4     
 
     */
 
    function validator()
 
    {
 
    }
 
    /**
 
     * check a number optional -,+,. values
 
     * @param   string        
 
     * @return  boolean
 
     */
 
    function is_numeric($val)
 
    {
 
        return (bool)preg_match('/^[-+]?[0-9]*.?[0-9]+$/', $val);
 
    }
 
    /**
 
     * valid email     
 
     * @param   string   
 
     * @return  boolean
 
     */
 
    function is_email($val)
 
    {
 
        return (bool)(preg_match("/^([a-z0-9+_-]+)(.[a-z0-9+_-]+)*@([a-z0-9-]+.)+[a-z]{2,6}$/i",
 
            $val));
 
    }
 
    /**
 
     * Valid URL or web address
 
     * @param   string      
 
     * @return  boolean
 
     */
 
    function is_url($val)
 
    {
 
        return (bool)preg_match("^((((https?|ftps?|gopher|telnet|nntp)://)|(mailto:|news:))(%[0-9A-Fa-f]{2}|[-()_.!~*';/?:@&=+$,A-Za-z0-9])+)([).!';/?:,][[:blank:]])?$",
 
            $val);
 
    }
 
    /**
 
     * Valid IP address
 
     * @param   string   
 
     * @return  boolean
 
     */
 
    function is_ipaddress($val)
 
    {
 
        return (bool)preg_match("/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/",
 
            $val);
 
    }
 
    /**
 
     * Matches only alpha letters
 
     * @param   string   
 
     * @return  boolean
 
     */
 
    function is_alpha($val)
 
    {
 
        return (bool)preg_match("/^([a-zA-Z])+$/i", $val);
 
    }
 
    /**
 
     * Matches alpha and numbers only
 
     * @param   string   
 
     * @return  boolean
 
     */
 
    function is_alphanumeric($val)
 
    {
 
        return (bool)preg_match("/^([a-zA-Z0-9])+$/i", $val);
 
    }
 
    /**
 
     * Matches alpha ,numbers,-,_ values
 
     * @param   string  
 
     * @return  boolean
 
     */
 
    function is_alphanumericdash($val)
 
    {
 
        return (bool)preg_match("/^([-a-zA-Z0-9_-])+$/i", $val);
 
    }
 
    /**
 
     * Matches alpha and dashes like -,_
 
     * @param   string  
 
     * @return  boolean
 
     */
 
    function is_alphadash($val)
 
    {
 
        return (bool)preg_match("/^([A-Za-z_-])+$/i", $val);
 
    }
 
    /**
 
     *Matches exactly number
 
     * @param   string   
 
     * @return  boolean
 
     */
 
    function is_integer($val)
 
    {
 
        return is_int($val);
 
    }
 
    /**
 
     * Valid Credit Card
 
     * @param   string   
 
     * @return  boolean
 
     */
 
    function is_creditcard($val)
 
    {
 
        return (bool)preg_match("/^((4d{3})|(5[1-5]d{2})|(6011)|(7d{3}))-?d{4}-?d{4}-?d{4}|3[4,7]d{13}$/",
 
            $val);
 
    }
 
    /**
 
     * check given string length is between given range 
 
     * @param   string   
 
     * @return  boolean
 
     */
 
    function is_rangelength($val, $min = '', $max = '')
 
    {
 
        return (strlen($val) >= $min and strlen($val) <= $max);
 
    }
 
    /**
 
     *Check the string length has minimum length
 
     * @param   string   
 
     * @return  boolean
 
     */
 
    function is_minlength($val, $min)
 
    {
 
        return (strlen($val) >= (int)$min);
 
    }
 
    /**
 
     * check string length exceeds maximum length     
 
     * @param   string   
 
     * @return  boolean
 
     */
 
    function is_maxlength($val, $max)
 
    {
 
        return (strlen($val) <= (int)$max);
 
    }
 
    /**
 
     * check given number exceeds max values   
 
     * @param   string   
 
     * @return  boolean
 
     */
 
    function is_maxvalue($number,$max)
 
    {
 
         return ($number >$max);
 
    }
 
    /**
 
     * check given number below value   
 
     * @param   string   
 
     * @return  boolean
 
     */
 
    function is_minvalue($number)
 
    {
 
        return ($number < $max);
 
    }
 
    /**
 
     * check given number between given values
 
     * @param   string   
 
     * @return  boolean
 
     */
 
    function is_rangevalue($number,$min,$max)
 
    {
 
        return ($number >$min and $number<$max);
 
    }
 
    /**
 
     * check for exactly length of string
 
     * @param   string  
 
     * @return  boolean
 
     */
 
    function is_length($val, $length)
 
    {
 
        return (strlen($val) == (int)$length);
 
    }
 
    /**
 
     * check decimal with . is optional and after decimal places up to 6th precision
 
     * @param   string   
 
     * @return  boolean
 
     */
 
    function is_decimal($val)
 
    {
 
        return (bool)preg_match("/^d+(.d{1,6})?$/'", $val);
 
    }
 
    /**
 
     * Valid hexadecimal color ,that may have #,
 
     * @param   string   
 
     * @return  boolean
 
     */
 
    function is_hexcolor($color)
 
    {
 
        return (bool)preg_match('/^#?+[0-9a-f]{3}(?:[0-9a-f]{3})?$/i', $color);
 
    }
 
    /**
 
     * Matches  againest given regular expression ,including delimeters
 
     * @param   string   
 
     * @return  boolean
 
     */
 
    function is_regex($val, $expression)
 
    {
 
        return (bool)preg_match($expression, (string )$val);
 
    }
 
    /**
 
     * compares two any kind of values ,stictly
 
     * @param   string   
 
     * @return  boolean
 
     */
 
    function is_matches($val, $value)
 
    {
 
        return ($val === $value);
 
    }
 
    /**
 
     * check if field empty string ,orject,array
 
     * @param   string   
 
     * @return  boolean
 
     */
 
    function is_empty($val)
 
    {
 
        return in_array($val, array(null, false, '', array()), true);
 
    }
 
    /**
 
     * Check if given string matches any format date
 
     * @param   string   
 
     * @return  boolean
 
     */
 
    function is_date($val)
 
    {
 
        return (strtotime($val) !== false);
 
    }
 
    /**
 
     * check given string againest given array values
 
     * @param   string   
 
     * @return  boolean
 
     */
 
    function is_enum($val, $arr)
 
    {
 
        return in_array($val, $arr);
 
    }
 
    /**
 
     * Checks that a field matches a v2 md5 string
 
     * @param   string   
 
     * @return  boolean
 
     */
 
    function is_md5($val)
 
    {
 
        return (bool)preg_match("/[0-9a-f]{32}/i", $val);
 
    }
 
    /**
 
     * Matches base64 enoding string
 
     * @param   string   
 
     * @return  boolean
 
     */
 
    function is_base64($val)
 
    {
 
        return (bool)!preg_match('/[^a-zA-Z0-9/+=]/', $val);
 
    }
 
    /**
 
     * check if array has unique elements,it must have  minimum one element
 
     * @param   string   
 
     * @return  boolean
 
     */
 
    function is_unique($arr)
 
    {
 
        $arr = (array )$arr;
 
        $count1 = count($arr);
 
        $count2 = count(array_unique($arr));
 
        return (count1 != 0 and (count1 == $count2));
 
    }
 
    /**
 
     * Check is rgb color value
 
     * @param   string   
 
     * @return  boolean
 
     */
 
    function is_rgb($val)
 
    {
 
        return (bool)preg_match("/^(rgb(s*b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])bs*,s*b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])bs*,s*b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])bs*))|(rgb(s*(d?d%|100%)+s*,s*(d?d%|100%)+s*,s*(d?d%|100%)+s*))$/",
 
            $val);
 
    }
 
    /**
 
     * is given field is boolean value or not
 
     * @param   string   
 
     * @return  boolean
 
     */
 
    function is_boolean($val)
 
    {
 
        $booleans = array(1, 0, '1', '0', true, false, true, false);
 
        $literals = array('true', 'false', 'yes', 'no');
 
        foreach ($booleans as $bool) {
 
            if ($val === $bool)
 
                return true;
 
        }
 
        return in_array(strtolower($val), $literals);
 
    } 
 
    /**
 
     * A token that don't have any white space
 
     * @param   string   
 
     * @return  boolean
 
     */
 
    function is_token($val)
 
    {
 
        return (bool)!preg_match('/s/', $val);
 
    }
 
    /**
 
     * Checks that a field is exactly the right length.
 
     * @param   string   value
 
     * @link  http://php.net/checkdnsrr  not added to Windows until PHP 5.3.0
 
     * @return  boolean
 
     */
 
    function is_emaildomain($email)
 
    {
 
        return (bool)checkdnsrr(preg_replace('/^[^@]++@/', '', $email), 'MX');
 
    }
 
    /**
 
     * Matches a phone number that length optional numbers 7,10,11
 
     * @param   string   
 
     * @return  boolean
 
     */
 
    function is_phone($number, $lengths = null)
 
    {
 
        if (!is_array($lengths)) {
 
            $lengths = array(7, 10, 11);
 
        }
 
        $number = preg_replace('/D+/', '', $number);
 
        return in_array(strlen($number), $lengths);
 
    }
 
    /**
 
     * check given sting is UTF8 
 
     * @param   string  
 
     * @return  boolean
 
     */
 
    function is_utf8($val)
 
    {
 
        return preg_match('%(?:
 
        [xC2-xDF][x80-xBF]        
 
        |xE0[xA0-xBF][x80-xBF]               
 
        |[xE1-xECxEExEF][x80-xBF]{2}     
 
        |xED[x80-x9F][x80-xBF]               
 
        |xF0[x90-xBF][x80-xBF]{2}   
 
        |[xF1-xF3][x80-xBF]{3}                  
 
        |xF4[x80-x8F][x80-xBF]{2}    
 
        )+%xs', $val);
 
    }
 
    /**
 
     * Given sting is lower cased
 
     * @param   string   
 
     * @return  boolean
 
     */
 
    function is_lower($val)
 
    {
 
        return (bool)preg_match("/^[a-z]+$/", $val);
 
    }
 
    /**
 
     * Given string is upper cased?
 
     * @param   string   
 
     * @return  boolean
 
     */
 
    function is_upper($val)
 
    {
 
        return (bool)preg_match("/^[A-Z]+$/", $val);
 
    }
 
    /**
 
     * Checks that given value matches following country pin codes.     
 
     * at = austria
 
     * au = australia
 
     * ca = canada
 
     * de = german
 
     * ee = estonia
 
     * nl = netherlands
 
     * it = italy
 
     * pt = portugal
 
     * se = sweden
 
     * uk = united kingdom
 
     * us = united states
 
     * @param String   
 
     * @param String
 
     * @return  boolean
 
     */
 
    function is_pincode($val, $country = 'us')
 
    {
 
        $patterns = array('at' => '^[0-9]{4,4}$', 'au' => '^[2-9][0-9]{2,3}$', 'ca' =>
 
            '^[a-zA-Z].[0-9].[a-zA-Z].s[0-9].[a-zA-Z].[0-9].', 'de' => '^[0-9]{5,5}$', 'ee' =>
 
            '^[0-9]{5,5}$', 'nl' => '^[0-9]{4,4}s[a-zA-Z]{2,2}$', 'it' => '^[0-9]{5,5}$',
 
            'pt' => '^[0-9]{4,4}-[0-9]{3,3}$', 'se' => '^[0-9]{3,3}s[0-9]{2,2}$', 'uk' =>
 
            '^([A-Z]{1,2}[0-9]{1}[0-9A-Z]{0,1}) ?([0-9]{1}[A-Z]{1,2})$', 'us' =>
 
            '^[0-9]{5,5}[-]{0,1}[0-9]{4,4}$');
 
        if (!array_key_exists($country, $patterns))
 
            return false;
 
        return (bool)preg_match("/" . $patterns[$country] . "/", $val);
 
    }
 
    /**
 
     * Check given url really exists?
 
     * @param   string   
 
     * @return  boolean
 
     */
 
    function is_urlexists($link)
 
    {
 
        if (!$this->is_url($link))
 
            return false;
 
        return (bool)@fsockopen($link, 80, $errno, $errstr, 30);
 
    }
 
    /**
 
     * Check given sting has script tags
 
     * @param   string   
 
     * @return  boolean
 
     */
 
    function is_jssafe($val)
 
    {
 
        return (bool)(!preg_match("/<script[^>]*>[srn]*(<!--)?|(-->)?[srn]*</script>/",
 
            $val));
 
    }
 
    /**
 
     * given sting has html tags?
 
     * @param   string   
 
     * @return  boolean
 
     */
 
    function is_htmlsafe($val)
 
    {
 
        return (bool)(!preg_match("/<(.*)>.*</$1>/", $val));
 
    }
 
    /**
 
     * check given sring has multilines 
 
     * @param   string   
 
     * @return  boolean
 
     */
 
    function is_multiline($val)
 
    {
 
        return (bool)preg_match("/[nrt]+/", $val);
 
    }
 
    /**
 
     * check given array key element exists?
 
     * @param   string   
 
     * @return  boolean
 
     */
 
    function is_exists($val, $arr)
 
    {
 
        return isset($arr[$val]);
 
    }
 
    /**
 
     * is given string is ascii format?
 
     * @param   string        
 
     * @return  boolean
 
     */
 
    function is_ascii($val)
 
    {
 
        return !preg_match('/[^x00-x7F]/i', $val);
 
    }
 
    /**
 
     * Checks given value again MAC address of the computer
 
     * @param   string   value      
 
     * @return  boolean
 
     */
 
    function is_macaddress($val)
 
    {
 
        return (bool)preg_match('/^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$/',
 
            $val);
 
    }
 
    /**
 
     * Checks given value matches us citizen social security number
 
     * @param   string         
 
     * @return  boolean
 
     */
 
    function is_usssn($val)
 
    {
 
        return (bool)preg_match("/^d{3}-d{2}-d{4}$/", $val);
 
    }
 
    /**
 
     * Checks given value matches date de
 
     * @param   string         
 
     * @return  boolean
 
     */
 
    function is_dateDE($date)
 
    {
 
        return (bool)preg_match("/^dd?.dd?.ddd?d?$/", $date);
 
    }
 
    /**
 
     * Checks given value matches us citizen social security number
 
     * @param   string         
 
     * @return  boolean
 
     */
 
    function is_dateISO($date)
 
    {
 
        return (bool)preg_match("/^d{4}[/-]d{1,2}[/-]d{1,2}$/", $date);
 
    }
 
    /**
 
     * Checks given value matches a time zone  
 
     * +00:00 | -05:00 
 
     * @param   string         
 
     * @return  boolean
 
     */
 
    function is_timezone($val)
 
    {
 
        return (bool)preg_match("/^[-+]((0[0-9]|1[0-3]):([03]0|45)|14:00)$/", $val);
 
    }
 
    /**
 
     * Time in 24 hours format with optional seconds
 
     * 12:15 | 10:26:59 | 22:01:15 
 
     * @param   string         
 
     * @return  boolean
 
     */
 
    function is_time24($val)
 
    {
 
        return (bool)preg_match("/^(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9])(:([0-5]?[0-9]))?$/",
 
            $val);
 
    }
 
    /**
 
     * Time in 12 hours format with optional seconds
 
     * 08:00AM | 10:00am | 7:00pm
 
     * @param   string         
 
     * @return  boolean
 
     */
 
    function is_time12($val)
 
    {
 
        return (bool)preg_match("/^([1-9]|1[0-2]|0[1-9]){1}(:[0-5][0-9][aApP][mM]){1}$/",
 
            $val);
 
    }
 
}
 
PHP本身自带的有一个readdir的函数,不过只能读取当前的目录,根据这个函数,我写了另外一个函数,用来实现我的需求。函数的原理很简单,主要就是用了一下递归调用。
 代码如下 复制代码

<?php
class listdir{
var $depth;
var $dirname;
var $list;
var $tostring;

function listdir($dir){
$this->dirname=$dir;
$this->depth=0;
$this->tostring=”";
}

//把结果保存进多维数组
function getlist($dir=”"){
if($dir==”")$dir=$this->dirname;
$d=@dir($dir);
while(false!==($item=$d->read()))
{
if($item!=”.”&&$item!=”..”)
{
$path=$dir.”/”.$item;
if(is_dir($path)){
$this->depth+=1;
$this->getlist($path);
}else{
$this->list[$this->depth][]=$item;
}
}
}
$this->list[$this->depth]['directory']=$dir;
$this->depth-=1;
$d->close();
return $this->list;
}

//字符窜化结果

function tostring($dir=”"){
if($dir==”")$dir=$this->dirname;
$d=@dir($dir);
$this->tostring.=”<UL>n”;
$this->tostring.=”Directory:”.$dir.”n”;
while(false!==($item=$d->read()))
{
if($item!=”.”&&$item!=”..”)
{
$path=$dir.”/”.$item;
if(is_dir($path)){
$this->depth+=1;
$this->tostring($path);
}else{
$this->tostring.=”<LI>”.$item.”</LI>n”;
}
}
}
$this->depth-=1;
$d->close();
$this->tostring.=”</UL>n”;
return $this->tostring;
}
}
$wapdir=”jquery”;
$d=new listdir($wapdir);
echo $d->tostring();
?>

递归创建多级目录

1.先判断最底层目录div/css/layout是否存在;2.判断div/css/layout的上层目录div/css是否存在,不存在则以div/css作为参数递归进行


下面是程序代码:

 代码如下 复制代码

function mkdirs($dir)
{
if(!is_dir($dir))
{
if(!mkdirs(dirname($dir))){
return false;
}
if(!mkdir($dir,0777)){
return false;
}
}
return true;
}
mkdirs('div/css/layout');


同样的思路,php用rmdir和unlink递归删除多级目录的代码:

function rmdirs($dir)
{
$d = dir($dir);
while (false !== ($child = $d->read())){
if($child != '.' && $child != '..'){
if(is_dir($dir.'/'.$child))
rmdirs($dir.'/'.$child);
else unlink($dir.'/'.$child);
}
}
$d->close();
rmdir($dir);
}

下面我们给出一个PHP正则表达式匹配字符串中的指定标签实现程序代码,有需要学习的朋友可参考本教程。

在 PHP 应用中,正则表达式主要用于:

•正则匹配:根据正则表达式匹配相应的内容
•正则替换:根据正则表达式匹配内容并替换
•正则分割:根据正则表达式分割字符串

常用

preg_match_all()
preg_match_all() 函数用于进行正则表达式全局匹配,成功返回整个模式匹配的次数(可能为零),如果出错返回 FALSE 。

语法:

int preg_match_all( string pattern, string subject, array matches [, int flags ] )


实例

$str = "之二宽阔的甘家口东#标签1#标签2 #标签3。#标签4,都发$1234 ¥xc,cvm , ¥12,dflksjf如何#标签5.x

 代码如下 复制代码
#tag6.cvxcv“";
preg_match_all('/#([a-zA-Z0-9x7f-xff]+)/', $str, $mat);
print_r($mat);
 
preg_match("/[x{00a5}x{ffe5}](d+)/u", $str, $mat);
print_r($mat);

正则匹配中文汉字根据页面编码不同而略有区别:

•GBK/GB2312编码:[x80-xff>]+ 或 [xa1-xff]+
•UTF-8编码:[x{4e00}-x{9fa5}]+/u
例子:

 代码如下 复制代码

<?php
$str = "学习php是一件快乐的事。";
preg_match_all("/[x80-xff]+/", $str, $match);
//UTF-8 使用:
//preg_match_all("/[x{4e00}-x{9fa5}]+/u", $str, $match);
print_r($match);
?>

补充说明:

双字节字符编码范围

1. GBK (GB2312/GB18030)
x00-xff GBK双字节编码范围
x20-x7f ASCII
xa1-xff 中文 gb2312
x80-xff 中文 gbk

2. UTF-8 (Unicode)

u4e00-u9fa5 (中文)
x3130-x318F (韩文
xAC00-xD7A3 (韩文)
u0800-u4e00 (日文)

[!--infotagslink--]

相关文章

  • C#开发Windows窗体应用程序的简单操作步骤

    这篇文章主要介绍了C#开发Windows窗体应用程序的简单操作步骤,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-12
  • C++调用C#的DLL程序实现方法

    本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
  • antdesign-vue结合sortablejs实现两个table相互拖拽排序功能

    这篇文章主要介绍了antdesign-vue结合sortablejs实现两个table相互拖拽排序功能,本文通过实例图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-01-09
  • 微信小程序 页面传值详解

    这篇文章主要介绍了微信小程序 页面传值详解的相关资料,需要的朋友可以参考下...2017-03-13
  • C#使用Process类调用外部exe程序

    本文通过两个示例讲解了一下Process类调用外部应用程序的基本用法,并简单讲解了StartInfo属性,有需要的朋友可以参考一下。...2020-06-25
  • JS+CSS实现分类动态选择及移动功能效果代码

    本文实例讲述了JS+CSS实现分类动态选择及移动功能效果代码。分享给大家供大家参考,具体如下:这是一个类似选项卡功能的选择插件,与普通的TAb区别是加入了动画效果,多用于商品类网站,用作商品分类功能,不过其它网站也可以用,...2015-10-21
  • 使用GruntJS构建Web程序之构建篇

    大概有如下步骤 新建项目Bejs 新建文件package.json 新建文件Gruntfile.js 命令行执行grunt任务 一、新建项目Bejs源码放在src下,该目录有两个js文件,selector.js和ajax.js。编译后代码放在dest,这个grunt会...2014-06-07
  • 微信小程序二维码生成工具 weapp-qrcode详解

    这篇文章主要介绍了微信小程序 二维码生成工具 weapp-qrcode详解,教大家如何在项目中引入weapp-qrcode.js文件,通过实例代码给大家介绍的非常详细,需要的朋友可以参考下...2021-10-23
  • uniapp微信小程序:key失效的解决方法

    这篇文章主要介绍了uniapp微信小程序:key失效的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-01-20
  • js实现列表按字母排序

    这篇文章主要为大家详细介绍了js实现列表按字母排序,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-08-11
  • C# 参数按照ASCII码从小到大排序(字典序)

    这篇文章主要介绍了C# 参数按照ASCII码从小到大排序(字典序)的方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • 将c#编写的程序打包成应用程序的实现步骤分享(安装,卸载) 图文

    时常会写用c#一些程序,但如何将他们和photoshop一样的大型软件打成一个压缩包,以便于发布....2020-06-25
  • PHP实现无限级分类(不使用递归)

    无限级分类在开发中经常使用,例如:部门结构、文章分类。无限级分类的难点在于“输出”和“查询”,例如 将文章分类输出为<ul>列表形式; 查找分类A下面所有分类包含的文章。1.实现原理 几种常见的实现方法,各有利弊。其中...2015-10-23
  • PHP常用的小程序代码段

    本文实例讲述了PHP常用的小程序代码段。分享给大家供大家参考,具体如下:1.计算两个时间的相差几天$startdate=strtotime("2009-12-09");$enddate=strtotime("2009-12-05");上面的php时间日期函数strtotime已经把字符串...2015-11-24
  • 微信小程序 网络请求(GET请求)详解

    这篇文章主要介绍了微信小程序 网络请求(GET请求)详解的相关资料,需要的朋友可以参考下...2016-11-22
  • 微信小程序自定义tabbar组件

    这篇文章主要为大家详细介绍了微信小程序自定义tabbar组件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-03-14
  • PHP实现递归无限级分类

    在一些复杂的系统中,要求对信息栏目进行无限级的分类,以增强系统的灵活性。那么PHP是如何实现无限级分类的呢?我们在本文中使用递归算法并结合mysql数据表实现无限级分类。 递归,简单的说就是一段程序代码的重复调用,当把...2015-10-23
  • 微信小程序如何获取图片宽度与高度

    这篇文章主要给大家介绍了关于微信小程序如何获取图片宽度与高度的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-10
  • ecshop商品无限级分类代码

    ecshop商品无限级分类代码 function cat_options($spec_cat_id, $arr) { static $cat_options = array(); if (isset($cat_options[$spec_cat_id]))...2016-11-25
  • 微信小程序(应用号)开发新闻客户端实例

    这篇文章主要介绍了微信小程序(应用号)开发新闻客户端实例的相关资料,需要的朋友可以参考下...2016-10-25