- $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
- <?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 的垃圾回收機制:html
- PHP垃圾回收機制是php5以後纔有的這<span id="transmark"></span>個東西,下面我來給你們介紹一下關於PHP垃圾回收機制一些理解,但願對各位同窗有所幫助。
-
- php 5.3以前使用的垃圾回收機制是單純的「引用計數」,也就是每一個內存對象都分配一個計數器,當內存對象被變量引用時,計數器 1;當變量引用撤掉後,計數器-1;當計數器=0時,代表內存對象沒有被使用,該內存對象則進行銷燬,垃圾回收完成。
-
- 「引用計數」存在問題,就是當兩個或多個對象互相引用造成環狀後,內存對象的計數器則不會消減爲0;這時候,這一組內存對象已經沒用了,可是不能回收,從而致使內存泄露;
-
- php5.3開始,使用了新的垃圾回收機制,在引用計數基礎上,實現了一種複雜的算法,來檢測內存對象中引用環的存在,以免內存泄露
第69題:將外網圖片本地化、將外網圖片下載到本地的原理java
- <?php
- copy("http://image.v1.cn/vodone/20150723/303862_0x0.jpg",'e:/wamp/www/303862_0x0.jpg');
- ?>
第70題:PHP緩存技術總結
第71題:xml文檔中的CDATAmysql
第72題:如何列出目錄下的全部模板文件?遍歷文件夾、遍歷文件、遍歷目錄及子目錄、遍歷文件夾及子文件夾c++
- <?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()函數的區別web
- <?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']);
-
- ?>
目錄: 5一、php中如何統計一維數組中全部值出現的次數?返回一個數組,其元素的鍵名是原數組的值;鍵值是該值在原數組中出現的次數 5二、php中如何統計字符串中每種字符的出現次數並排序? 5三、php中使用str_word_count()函數計算字符串中的單詞數? 5六、php中redis與memcached區別? 5七、php中memcached的使用案例? 5八、php中文件緩存的使用案例? 5九、php中redis使用案例? 60、php如何讀取php.ini配置文件中的配置選項的值? 6一、php中如何動態的調用一個函數? 6二、php中__call魔術方法的使用 6三、php中如何解析php.ini配置文件? 6五、php中的重寫與重載的區別? 6六、php中static靜態局部變量(不是數組)在函數中的使用 6七、php中session的內部機制實現原理 6八、php中的垃圾回收機制 6九、php如何將外網圖片本地化或將外網圖片下載到本地的原理? 70、php中緩存技術總結 7一、php中xml文檔的CDATA理解 7二、php中如何列出目錄下的全部模板文件?遍歷文件夾、遍歷文件、遍歷目錄及子目錄、遍歷文件夾及子文件夾 7四、php中遍歷函數:scandir()與glob()函數的區別? 7五、php中經過var_export()函數返回數組格式的字符串