PHP技术人员-口试总结PHP篇
PHP技术人员--面试总结PHP篇
1、实现中文字符串截取无乱码方法
开启mbstring扩展,然后自定义函数:
<?php header('content-Type:text/html:charset=utf-8'); function substr_utf8($str, $start, $length = null) { return join("", array_slice( preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY), $start, $length) ); } //实例 (PS:^_^不错的php学习交流群:276167802,验证:csl,有兴趣的话可以加入进来一起讨论) $str = "我是一个good男孩!"; echo substr_utf8($str, 2, 4);
2、用PHP打印前一天的时间
<?php header('content-Type:text/html:charset=utf-8'); echo date('Y-m-d H:i:s',strtotime('-1 day'));
3、不适用第三个变量交换2个变量的值
<?php header('content-Type:text/html:charset=utf-8'); $a = 'a'; $b = 'b'; list($a,$b) = array($b,$a); echo $a,$b;
4、将1234567890,转换成1,234,567,890
header('content-Type:text/html:charset=utf-8'); $str = '1234567890'; //反转字符串 $str = strrev($str); //使用逗号分隔得到098,765,432,1, $str = chunk_split($str,3,','); //再次反转 $str = strrev($str); //去掉左边的, $str = ltrim($str,','); echo $str;
5、实现utf8字符串反转
不能使用strrev,中文会出错
function strrev_utf8($str){ return join("",array_reverse(preg_split("//u",$str))); } $str = "我是一个good男孩"; echo strrev_utf8($str); 6、取url的文件扩展名,尽量多的去实现方法 $str = "www.baidu.com/index.php"; function get_ext1($str){ return strrchr($str,'.'); } function get_ext2($str){ return substr($str,strrpos($str,'.')); } function get_ext3($str){ $str = pathinfo($str); return $str['extension']; } function get_ext4($str){ $arr = explode('.',$str); return $arr[count($arr)-1]; } function get_ext5($str){ $pattern = '/^[^\.]+\.([\w]+)$/'; return preg_replace($pattern,'${1}',basename($str)); }
7、写一个函数,将字符串open_door转换为OpenDoor
$str = "open_door"; function change_str($str){ $arr = explode('_',$str); $arr = array_map('ucfirst',$arr); return implode('',$arr); } echo change_str($str);
8、单例模式
<?php class Mysql{ private static $instance = null; private $conn; //设置为私有,不允许通过new获得对象 private function __construct(){ $conn = mysql_connect('localhost','root','123456'); } //获取实例方法 public static function getInstance(){ if(! self::$instance instanceof self){ self::$instance = new self; } return self::$instance; } //禁止克隆 private function __clone(){} } $db = Mysql::getInstance(); 9、写一段PHP代码,确保多个进程同时写入同一个文件成功 <?php $fp = fopen("lock.txt","w+"); if(flock($fp,LOCK_EX)){ //获得写锁 fwrite($fp,'write something'); flock($fp,LOCK_UN); }else{ echo "file is locking..."; } fclose($fp);
10、从一个完成的url获取文件扩展名
<?php $url = 'http://www.baidu.com/a/b/index.php?id=1'; $arr = parse_url($url); $fname = basename($arr['path']); $arr = explode('.',$fname); echo $arr[count($arr)-1];
11、写一个函数可以便利一个文件夹下的所有文件和子文件夹
<?php function my_scandir($dir){ $files = array(); if(is_dir($dir)){ if($handle = opendir($dir)){ while(($file = readdir($handle)) !== false){ if($file != "." && $file != ".."){ if(is_dir($dir.'/'.$file)){ $files[$file] = my_scandir($dir.'/'.$file); }else{ $files[] = $dir.'/'.$file; } } } closedir($handle); return $files; } } } var_dump(my_scandir('D:\wamp\www\study'));
12、论坛中无限分类实现原理
首先设计数据库表
create table category( cate_id int unsigned not null auto_increment primary key, cat_name varchar(30) not null default '', parent_id int unsigned not null default 0 ) engine=innodb charset=utf8; 然后用函数去递归实现,无限分类 function tree($arr,$pid=0,$level=0){ static $list = array(); foreach($arr as $v){ //如果是顶级分类,则存入$list //然后以此节点为根几点,遍历其子节点 if($v['parent_id'] == $pid){ $v['level'] = $level; $list[] = $v; tree($arr,$v['cat_id'],$level+1); } } return $list; }
13、计算2个文件的相对路径
<?php $a = '/a/b/c/d/a.php'; $b = '/a/b/e/f/b.php'; $arr1 = explode('/',dirname($a)); $arr2 = explode('/',dirname($b)); for($i=0,$len=count($arr2);$i<$len;$i++){ if($arr1[$i] != $arr2[$i]){ break; } } //不在用一个根目录 if($i == 1){ $ret = array(); } //在同一个根目录下 if($i != 1 && $i < $len){ $ret = array_fill(0,$len-$i,".."); } //在同一个目录下 if($i == $len){ $ret = array('./'); } $ret = array_merge($ret,array_slice($arr1,$i)); echo implode('/',$ret); 14、约瑟夫环问题 <?php function king($n,$m){ $monkey = range(1,$n); $i = 0; while(count($monkey) > 1){ $i += 1; $head = array_shift($monkey);//一个个出列最前面的 if( $i % $m != 0){ //如果不是m的倍数,则返回尾部,否则就出列了 array_push($monkey,$head); } } return $monkey[0]; } echo king(10,7);
15、PHP实现双向队列
<?php class Dqueue{ private $queue = array(); public function addFirst($item){ return array_unshift($this->queue,$item); } public function addLast($item){ return array_push($this->queue,$item); } public function getFirst(){ return array_shift($this->queue); } public function getLast(){ return array_pop($this->queue); } }
希望本文对广大php开发者有所帮助,感谢阅读本文。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The usage of return in C language is: 1. For functions whose return value type is void, you can use the return statement to end the execution of the function early; 2. For functions whose return value type is not void, the function of the return statement is to end the execution of the function. The result is returned to the caller; 3. End the execution of the function early. Inside the function, we can use the return statement to end the execution of the function early, even if the function does not return a value.

Function means function. It is a reusable code block with specific functions. It is one of the basic components of a program. It can accept input parameters, perform specific operations, and return results. Its purpose is to encapsulate a reusable block of code. code to improve code reusability and maintainability.

Source code: publicclassReturnFinallyDemo{publicstaticvoidmain(String[]args){System.out.println(case1());}publicstaticintcase1(){intx;try{x=1;returnx;}finally{x=3;}}}#Output The output of the above code can simply conclude: return is executed before finally. Let's take a look at what happens at the bytecode level. The following intercepts part of the bytecode of the case1 method, and compares the source code to annotate the meaning of each instruction in

Title: Example of using the Array.Sort function to sort an array in C# Text: In C#, array is a commonly used data structure, and it is often necessary to sort the array. C# provides the Array class, which has the Sort method to conveniently sort arrays. This article will demonstrate how to use the Array.Sort function in C# to sort an array and provide specific code examples. First, we need to understand the basic usage of the Array.Sort function. Array.So

In this article, we will learn about enumerate() function and the purpose of “enumerate()” function in Python. What is the enumerate() function? Python's enumerate() function accepts a data collection as a parameter and returns an enumeration object. Enumeration objects are returned as key-value pairs. The key is the index corresponding to each item, and the value is the items. Syntax enumerate(iterable,start) Parameters iterable - The passed in data collection can be returned as an enumeration object, called iterablestart - As the name suggests, the starting index of the enumeration object is defined by start. if we ignore

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

When programming in PHP, we often need to merge arrays. PHP provides the array_merge() function to complete array merging, but when the same key exists in the array, this function will overwrite the original value. In order to solve this problem, PHP also provides an array_merge_recursive() function in the language, which can merge arrays and retain the values of the same keys, making the program design more flexible. array_merge

Detailed explanation of the role and function of the MySQL.proc table. MySQL is a popular relational database management system. When developers use MySQL, they often involve the creation and management of stored procedures (StoredProcedure). The MySQL.proc table is a very important system table. It stores information related to all stored procedures in the database, including the name, definition, parameters, etc. of the stored procedures. In this article, we will explain in detail the role and functionality of the MySQL.proc table
