php面试题汇集2
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)
);
}
//实例
$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面试题汇集2的更多相关文章
- C++面试题汇集
1.在C++ 程序中调用被C 编译器编译后的函数,为什么要加extern “C”?答:首先,extern是C/C++语言中表明函数和全局变量作用范围的关键字,该关键字告诉编译器,其声明的函数和变量可以 ...
- Android面试题集合
https://www.jianshu.com/p/718aa3c1a70b https://www.jianshu.com/p/2dd855aa1938 https://www.jianshu.co ...
- 2019年阿里java面试题
一.JVM与性能优化 描述一下 JVM 加载 Class 文件的原理机制? 什么是类加载器? 类加载器有哪些? 什么是tomcat类加载机制? 类加载器双亲委派模型机制? Java 内存分配? Jav ...
- 反应器模式 vs 观察者模式
反应器模式(Reactor pattern)与观察者模式(Observer pattern) 反应器模式 是一种为处理服务请求并发提交到一个或者多个服务处理程序的事件设计模式.当请求抵达后,服务处理程 ...
- 反应器模式 vs 生产者消费者模式
相似点: 从结构上,反应器模式有点类似生产者消费者模式,即有一个或多个生产者将事件放入一个Queue中,而一个或多个消费者主动的从这个Queue中Poll事件来处理: 不同点: Reactor模式则并 ...
- Pipeline inbound(netty源码7)
netty源码死磕7 Pipeline 入站流程详解 1. Pipeline的入站流程 在讲解入站处理流程前,先脑补和铺垫一下两个知识点: (1)如何向Pipeline添加一个Handler节点 ( ...
- 一道常被人轻视的前端JS面试题
前言 年前刚刚离职了,分享下我曾经出过的一道面试题,此题是我出的一套前端面试题中的最后一题,用来考核面试者的JavaScript的综合能力,很可惜到目前为止的将近两年中,几乎没有人能够完全答对,并非多 ...
- JavaScript面试题
一道常被人轻视的前端JS面试题 标签(空格分隔): JavaScript function Foo() { getName = function () { alert (1); }; return t ...
- 一道常被人轻视的前端JS面试题(转)
分享下我曾经出过的一道面试题,此题是我出的一套前端面试题中的最后一题,用来考核面试者的JavaScript的综合能力,很可惜到目前为止的将近两年中,几乎没有人能够完全答对,并非多难只是因为大多面试者过 ...
随机推荐
- Mac 通过gem安装CocoaPods及Pod的使用
注:根据http://www.jianshu.com/p/6e5c0f78200a的文章做了部分修改 一.什么是CocoaPods CocoaPods是iOS项目的依赖管理工具,该项目源码在Githu ...
- node-mysql中防止SQL注入
备注: 本文针对mysqljs/mysql. 为了防止SQL注入,可以将SQL中传入参数进行编码,而不是直接进行字符串拼接.在node-mysql中,防止SQL注入的常用方法有以下四种: 方法一:使用 ...
- 以女朋友为例讲解 TCP/IP 三次握手与四次挥手
背景 和女朋友异地恋一年多,为了保持感情我提议每天晚上视频聊天一次. 从好上开始,到现在,一年多也算坚持下来了. 问题 有时候聊天的过程中,我的网络或者她的网络可能会不好,视频就会卡住,听不到对方的声 ...
- VMware安装完后,没有虚拟网卡
1 问题描述: 1.1 windows10首次安装VMware,或者非首次安装VMware时,安装后,没有出现如下图所示的虚拟网卡: 1.2 Xshell或者SecureCRT 或者editplus等 ...
- Madgwick算法详细解读
Madgwick算法详细解读 极品巧克力 前言 接上一篇文章<Google Cardboard的九轴融合算法>. Madgwick算法是另外一种九轴融合的方法,广泛应用在旋翼飞行器上,效果 ...
- nginx相关教程
1.nginx简易的教程 http://www.cnblogs.com/jingmoxukong/p/5945200.html#%E8%B7%A8%E5%9F%9F%E8%A7%A3%E5%86%B3 ...
- unary_function和binary_function详解
1.unary_function和binary_function介绍 1.1 unary_function介绍 unary_function可以作为一个一元函数对象的基类,它只定义了参数和返回值的类型 ...
- a Concise Sparse Matrix package
简明稀疏矩阵包 https://github.com/kulhanek/csparse https://github.com/kulhanek/csparse
- RAW编程接口
LWIP移植好之后,就要使用它提供的API接口来编写程序.
- MySQL 授权,回收权限,查看权限
show GRANTS for root@localhost;flush privileges;SHOW PROCESSLIST; #全局授权,回收权限GRANT ALL ON *.* TO 'tes ...