- $array=array(4,5,1,2,3,1,2,"a","a");
-
- $ac=array_count_values($array);
-
- echo "<pre>";print_r($ac);
- function countStr($str){
- $str_array=str_split($str);
- $str_array=array_count_values($str_array);
- arsort($str_array);
- return $str_array;
- }
- $str="asdfgfdas323344##$\$fdsdfg*$**$*$**$$443563536254fas";
- echo "<pre>";print_r(countStr($str));
-
- echo str_word_count("Hello world!");
-
- echo "<pre>";print_r(str_word_count("Hello world!",1));
-
- echo "<pre>";print_r(str_word_count("Hello world!",2));
- <?php
-
- function get_data(){
- $sql = "SELECT * from order_master ";
- return $this->db->cache_all($sql);
- }
-
- public function cache_all($sql, $reload = false, $limit = null) {
- $this->reload = $reload;
- $sql = $this->get_query_sql($sql, $limit);
- return $this->get_cache($sql, ‘get_all‘);
- }
-
- protected function get_cache($sql,$method) {
- $cache_file = md5($sql.$method);
- $res = $this->cache->get($cache_file);
- if(!$res) {
- $res= $this->$method($sql);
- if($res && $this->cache_mark && !$this->reload) {
- $this->cache->set($cache_file, $res);
- }
- }
- return $res;
- }
-
- public function get_all($sql, $limit = null, $fetch_mode = MYSQLI_ASSOC) {
- $this->query($sql, $limit);
- $all_rows = array();
- $this->fetch_mode = $fetch_mode;
- while($rows = $this->fetch()) {
- $all_rows[] = $rows;
- }
- $this->free();
- return $all_rows;
- }
- ?>
- public function &get($key, $cache_time = ‘‘) {
- if(empty ($cache_time)) {
- $cache_time = $this->cache_time;
- } else {
- $cache_time = intval($cache_time);
- }
- $cache_encode_key = $this->get_key($key);
-
- $filename = $this->cache_dir.‘/‘.substr($cache_encode_key,0,2).‘/‘.substr($cache_encode_key,2,2).‘/‘.$cache_encode_key.".cache.php";
- if(!is_file($filename) || time() - filemtime($filename) > $cache_time) {
- return false;
- } else {
- return unserialize(file_get_contents($filename));
- }
- }
- public function set($key,$data) {
- $cache_encode_key = $this->get_key($key);
- $cache_dir = $this->cache_dir.‘/‘.substr($cache_encode_key,0,2).‘/‘.substr($cache_encode_key,2,2).‘/‘;
- $filename = $cache_dir.$cache_encode_key.".cache.php";
- if( $this->mkpath($cache_dir) ) {
- $out = serialize($data);
- file_put_contents($filename, $out);
- }
- }
- public function add($key, $data) {
- $cache_encode_key = $this->get_key($key);
- $cache_dir = $this->cache_dir.‘/‘.substr($cache_encode_key,0,2).‘/‘.substr($cache_encode_key,2,2).‘/‘;
- $filename = $cache_dir.$cache_encode_key.".cache.php";
- if(is_file($filename)) {
- return false;
- } else {
- $this->set($key, $data);
- }
- }
- public function del($key) {
- $cache_encode_key = $this->get_key($key);
- $cache_dir = $this->cache_dir.‘/‘.substr($cache_encode_key,0,2).‘/‘.substr($cache_encode_key,2,2).‘/‘;
- $filename = $cache_dir.$cache_encode_key.".cache.php";
- if(is_file($filename)) {
- return false;
- } else {
- @unlink($filename);
- }
- }
-
- public function get_key($key){
- return md5(CACHE_KEY.$key);
- }
-
- public function push_activity_goods_online($data) {
- $key = sprintf($this->config->item(‘activity_goods_push_key‘), $data[‘goods_id‘]);
-
- $this->cache->redis->delete($key);
- $this->load->driver(‘cache‘, array(‘adapter‘ => ‘redis‘));
- return $this->cache->redis->hmset($key, $data);
- }
-
-
-
- public function pull_activity_goods_online($key) {
- $key = sprintf($this->config->item(‘activity_goods_push_key‘), $key);
- $this->load->driver(‘cache‘, array(‘adapter‘ => ‘redis‘));
-
- return $this->cache->redis->hgetall($key);
- }
-
-
-
- public function del_activity_goods_online($key) {
- $key = sprintf($this->config->item(‘activity_goods_push_key‘), $key);
- $this->load->driver(‘cache‘, array(‘adapter‘ => ‘redis‘));
- return $this->cache->redis->delete($key);
- }
-
-
-
- public function push_activity_online($data) {
-
- $activity_push_key = $this->config->item(‘activity_push_key‘);
-
- if (isset($data[‘plateform‘]) && ($data[‘plateform‘]) != ‘all‘) {
- $activity_push_key = $data[‘plateform‘] . ‘:‘ . $activity_push_key;
- }
-
-
- if ($data[‘activity_range‘] == 1) {
- $key = sprintf($activity_push_key, ‘order‘);
- } else {
- $key = sprintf($activity_push_key, $data[‘activity_id‘]);
- }
-
- $this->cache->redis->delete($key);
- $this->load->driver(‘cache‘, array(‘adapter‘ => ‘redis‘));
- return $this->cache->redis->hmset($key, $data);
- }
-
-
-
- public function pull_activity_online($key) {
- if ($key == ‘order‘) {
- $key = sprintf($this->config->item(‘activity_push_key‘), ‘order‘);
- }if ($key == ‘mobile:order‘) {
- $key = "mobile:" . sprintf($this->config->item(‘activity_push_key‘), ‘order‘);
- } else {
- $key = sprintf($this->config->item(‘activity_push_key‘), $key);
- }
-
- $this->load->driver(‘cache‘, array(‘adapter‘ => ‘redis‘));
-
- return $this->cache->redis->hgetall($key);
- }
-
-
-
- public function del_activity_online($key, $activity = array()) {
- $activity_push_key = $this->config->item(‘activity_push_key‘);
-
-
- if (isset($activity[‘plateform‘]) && ($activity[‘plateform‘]) != ‘all‘) {
- $activity_push_key = $activity[‘plateform‘] . ‘:‘ . $activity_push_key;
- }
- $key = sprintf($activity_push_key, $key);
- $this->load->driver(‘cache‘, array(‘adapter‘ => ‘redis‘));
-
- return $this->cache->redis->delete($key);
- }
-
-
- public function pull_all_activity_online() {
- $aids = array();
- $key = "*" . sprintf($this->config->item(‘activity_push_key‘), "*");
- $this->load->driver(‘cache‘, array(‘adapter‘ => ‘redis‘));
- $hashname = $this->cache->redis->keys($key);
- if (is_array($hashname)) {
- $prefix_key = str_replace("%s", ‘‘, $this->config->item(‘activity_push_key‘));
- foreach ($hashname as $key => $value) {
-
- $aid = str_replace($prefix_key, "", $value);
- $aids[] = $aid;
- }
- return $aids;
- } else {
- return null;
- }
- }
-
-
- public function pull_all_activity_goods_online() {
- $aids = array();
- $key = sprintf($this->config->item(‘activity_goods_push_key‘), "*");
- $this->load->driver(‘cache‘, array(‘adapter‘ => ‘redis‘));
- $hashname = $this->cache->redis->keys($key);
- if (is_array($hashname)) {
- $prefix_key = str_replace("%s", ‘‘, $this->config->item(‘activity_goods_push_key‘));
- foreach ($hashname as $key => $value) {
- $aid = str_replace($prefix_key, "", $value);
- $aids[] = $aid;
- }
- return $aids;
- } else {
- return null;
- }
- }
-
-
- public function get_activity_goods_online($product_id) {
- if (empty($product_id)) {
- return false;
- }
- $activity_goods = $this->pull_activity_goods_online($product_id);
-
- if (!empty($activity_goods)) {
- return $activity_goods;
- }
-
- return false;
- }
- echo ini_get(‘post_max_size‘);
- echo get_cfg_var(‘post_max_size‘);
- $name_list = ‘小强,张三,李四,王五,马六‘;
- $funName = ‘explode‘;
- echo "<pre>";print_r($funName(‘,‘,$name_list));
- echo "<pre>";print_r(call_user_func_array($funName,array(‘,‘,$name_list)));
-
- class MethodTest {
-
- public function __call($name,$arguments) {
- if(method_exists($this,$name)){
- return $this->$name();
- }else{
- echo "调用的方法 $name() 不存在;传递给 $name() 方法的参数列表如下:".implode(‘,‘,$arguments)."\n";
- }
- }
- }
- $obj = new MethodTest();
- $obj->runTest(‘李四‘,‘张三‘);
-
- (1)如:配置文件 test.ini
-
- [names]
- me = Robert
- you = Peter
-
- [urls]
- first = "http://www.example.com"
- second = "http://www.w3school.com.cn"
-
- (2)代码:<?php print_r(parse_ini_file("test.ini")); ?>
-
- 结果:
- Array(
- [me] => Robert
- [you] => Peter
- [first] => http:
- [second] => http:
- )
-
- (3)代码:<?php print_r(parse_ini_file("test.ini",true)); ?>
-
- 结果:
- Array(
- [names] => Array(
- [me] => Robert
- [you] => Peter
- )
- [urls] => Array(
- [first] => http:
- [second] => http:
- )
- )
- <?php
-
- class human{
- public function say($name){
- echo $name,‘ 吃了吗?<br />‘;
- }
- }
-
- class stu extends human{
-
-
- public function say(){
- echo ‘切克闹,卡猫百比<br />‘;
- }
-
- }
-
- $li=new stu();
- $li->say();
- $li->say(‘binghui‘);
-
-
-
- class Calc {
- public function area() {
-
- $args = func_get_args();
- if(count($args) == 1) {
- return 3.14 * $args[0] * $args[0];
- } else if(count($args) == 2) {
- return $args[0] * $args[1];
- } else {
- return ‘未知图形‘;
- }
- }
- }
- $calc = new Calc();
- echo $calc->area(10),‘<br />‘;
- echo $calc->area(5,8);
- ?>
第67题:session机制:
- <?php
-
-
-
-
-
- $SESS_DBHOST = "127.0.0.1";
- $SESS_DBNAME = "cms";
- $SESS_DBUSER = "root";
- $SESS_DBPASS = "";
-
- $SESS_DBH = "";
- $SESS_LIFE = get_cfg_var("session.gc_maxlifetime");
-
-
- function sess_open($save_path, $session_name) {
- global $SESS_DBHOST, $SESS_DBNAME, $SESS_DBUSER, $SESS_DBPASS, $SESS_DBH;
-
- if (! $SESS_DBH = mysql_pconnect($SESS_DBHOST, $SESS_DBUSER, $SESS_DBPASS)) {
- echo "<li>Can‘t connect to $SESS_DBHOST as $SESS_DBUSER";
- echo "<li>MySQL Error: " . mysql_error();
- die;
- }
-
- if (! mysql_select_db($SESS_DBNAME, $SESS_DBH)) {
- echo "<li>Unable to select database $SESS_DBNAME";
- die;
- }
-
- return true;
- }
-
-
- function sess_close() {
- return true;
- }
-
- function sess_read($key) {
- global $SESS_DBH, $SESS_LIFE;
-
- $qry = "SELECT value FROM session_tbl WHERE sesskey = ‘$key‘ AND expiry > " . time();
- $qid = mysql_query($qry, $SESS_DBH);
-
- if (list($value) = mysql_fetch_row($qid)) {
- return $value;
- }
-
- return false;
- }
-
-
- function sess_write($key, $val) { <span id="transmark"></span>
- global $SESS_DBH, $SESS_LIFE;
-
- $expiry = time() + $SESS_LIFE;
- $value = addslashes($val);
-
- $qry = "INSERT INTO session_tbl VALUES (‘$key‘, $expiry, ‘$value‘)";
- $qid = mysql_query($qry, $SESS_DBH);
-
- if (! $qid) {
- $qry = "UPDATE session_tbl SET expiry = $expiry, value = ‘$value‘ WHERE sesskey = ‘$key‘ AND expiry > " . time();
- $qid = mysql_query($qry, $SESS_DBH);
- }
-
- return $qid;
- }
-
- function sess_destroy($key) {
- global $SESS_DBH;
-
- $qry = "DELETE FROM session_tbl WHERE sesskey = ‘$key‘";
- $qid = mysql_query($qry, $SESS_DBH);
-
- return $qid;
- }
-
- function sess_gc($maxlifetime) {
- global $SESS_DBH;
-
- $qry = "DELETE FROM session_tbl WHERE expiry < " . time();
- $qid = mysql_query($qry, $SESS_DBH);
-
- return mysql_affected_rows($SESS_DBH);
- }
-
-
-
- session_set_save_handler(
- "sess_open",
- "sess_close",
- "sess_read",
- "sess_write",
- "sess_destroy",
- "sess_gc"
- );
- session_start();
- ?>
第68题:PHP 的垃圾回收机制:
- PHP垃圾回收机制是php5之后才有的这<span id="transmark"></span>个东西,下面我来给大家介绍一下关于PHP垃圾回收机制一些理解,希望对各位同学有所帮助。
-
- php 5.3之前使用的垃圾回收机制是单纯的“引用计数”,也就是每个内存对象都分配一个计数器,当内存对象被变量引用时,计数器 1;当变量引用撤掉后,计数器-1;当计数器=0时,表明内存对象没有被使用,该内存对象则进行销毁,垃圾回收完成。
-
- “引用计数”存在问题,就是当两个或多个对象互相引用形成环状后,内存对象的计数器则不会消减为0;这时候,这一组内存对象已经没用了,但是不能回收,从而导致内存泄露;
-
- php5.3开始,使用了新的垃圾回收机制,在引用计数基础上,实现了一种复杂的算法,来检测内存对象中引用环的存在,以避免内存泄露
第69题:将外网图片本地化、将外网图片下载到本地的原理
- <?php
- copy("http://image.v1.cn/vodone/20150723/303862_0x0.jpg",‘e:/wamp/www/303862_0x0.jpg‘);
- ?>
第70题:PHP缓存技术总结
第71题:xml文档中的CDATA
第72题:如何列出目录下的所有模板文件?遍历文件夹、遍历文件、遍历目录及子目录、遍历文件夹及子文件夹
- <?php
-
- echo "<pre>";
- print_r(glob(‘E:/wamp/www/phpcms/phpcms/templates/default/content/*.html‘));
-
-
- /**
- * Array
- (
- [0] => category.html
- [1] => category_download.html
- [2] => category_picture.html
- [3] => category_video.html
- [4] => download.html
- [5] => footer.html
- [6] => header.html
- [7] => header_min.html
- [8] => header_page.html
- [9] => index.html
- [10] => list.html
- [11] => list_download.html
- [12] => list_picture.html
- [13] => list_video.html
- [14] => message.html
- [15] => page.html
- [16] => play_list.html
- [17] => rss.html
- [18] => search.html
- [19] => show.html
- [20] => show_download.html
- [21] => show_picture.html
- [22] => show_video.html
- [23] => show_videolist.html
- [24] => tag.html
- [25] => tag_list.html
- [26] => video_album.html
- [27] => video_for_ck.html
- )
- */
- echo "<pre>";
- print_r(@array_map(‘basename‘,glob(‘E:/wamp/www/phpcms/phpcms/templates/default/content/*.html‘)));
-
-
- ?>
第74题:遍历函数:scandir()与glob()函数的区别
- <?php
-
- echo "<pre>";print_r(scandir(‘E:/wamp/www/phpcms/‘));
- echo "<pre>";print_r(glob(‘E:/wamp/www/phpcms/*‘));
- ?>
第75题:var_export()函数用法:
- <?php
-
- function string2array($data) {
- if($data == ‘‘) return array();
- @eval("\$array = $data;");
- return $array;
- }
-
- $data=‘array (
- \‘ishtml\‘ => \‘1\‘,
- \‘template_list\‘ => \‘default\‘,
- \‘page_template\‘ => \‘page\‘,
- \‘meta_title\‘ => \‘\‘,
- \‘meta_keywords\‘ => \‘\‘,
- \‘meta_description\‘ => \‘\‘,
- \‘category_ruleid\‘ => \‘1\‘,
- \‘show_ruleid\‘ => \‘\‘,
- \‘repeatchargedays\‘ => \‘1\‘,
- )‘;
-
- echo "<pre>";print_r(string2array($data));
-
-
-
-
- $arr=array(
- ‘username‘=>‘admin‘,
- ‘password‘=>‘123‘
- );
-
-
- $str=var_export($arr,true);
-
- echo $str;
-
- @eval("\$str = $str;");
- print_r($str[‘username‘]);
-
- ?>
目录:
51、php中如何统计一维数组中所有值出现的次数?返回一个数组,其元素的键名是原数组的值;键值是该值在原数组中出现的次数
52、php中如何统计字符串中每种字符的出现次数并排序?
53、php中使用str_word_count()函数计算字符串中的单词数?
56、php中redis与memcached区别?
57、php中memcached的使用案例?
58、php中文件缓存的使用案例?
59、php中redis使用案例?
60、php如何读取php.ini配置文件中的配置选项的值?
61、php中如何动态的调用一个函数?
62、php中__call魔术方法的使用
63、php中如何解析php.ini配置文件?
65、php中的重写与重载的区别?
66、php中static静态局部变量(不是数组)在函数中的使用
67、php中session的内部机制实现原理
68、php中的垃圾回收机制
69、php如何将外网图片本地化或将外网图片下载到本地的原理?
70、php中缓存技术总结
71、php中xml文档的CDATA理解
72、php中如何列出目录下的所有模板文件?遍历文件夹、遍历文件、遍历目录及子目录、遍历文件夹及子文件夹
74、php中遍历函数:scandir()与glob()函数的区别?
75、php中通过var_export()函数返回数组格式的字符串