image

编辑人: 流年絮语

calendar2025-06-08

message1

visits621

PHP面试题详解 算法方面的

题一:

/**

* 请以空格作为间隔,拆分字符串’Apple Orange Banana Strawberry’,组成数组$fruit,

* 数组中所有元素都用小写字母,并按照字母先后次序排序

*/

class sort

{

private $str;

public function __construct($str)

{

$this->str=strtolower($str);

}

private function explodes()

{

if(empty($this->str)) return array();

$arr=explode(” “,$this->str);

return is_array($arr)?$arr:array($arr);

}

public function sort()

{

$explode=$this->explodes();

sort($explode);

return $explode;

}

}

//$str=’Apple Orange Banana Strawberry’;

////$sortob=new sort($str);

////var_dump($sortob->sort());

复制代码题二:

/**

* 对于用户输入一串字符串$string,要求$string中只能包含大于0的数字和英文逗号,

* 请用正则 表达式验证,对于不符合要求的$string返回出错信息

*/

class regx

{

public static function check($str)

{

if(preg_match(“/^([1-9,])+$/”,$str))

{

return true;

}

return false;

}

}

$str=”12345,6″;

if(regx::check($str))

{

echo “suc”;

}

else

{

echo “fail”;

}

复制代码题三:

<?php

/**

* 请写一段程序,在服务器创建一个文件fruit.dat,将试题3中得到的数组写入到改文件中,

* 然后写一段程序从文件中读取并还原数组

* @author zhuwenqiong

*

*/

class sort

{

private $str;

public function __construct($str)

{

$this->str=strtolower($str);

}

private function explodes()

{

if(empty($this->str)) return array();

$arr=explode(” “,$this->str);

return is_array($arr)?$arr:array($arr);

}

public function sort()

{

$explode=$this->explodes();

sort($explode);

return $explode;

}

}

class file

{

private $sort=null;

private $filepath;

public function __construct($arrobj,$path)

{

$this->sort=$arrobj;

$this->filepath=$path;

}

private function getresource($filename,$mode)

{

return fopen($this->filepath.$filename,$mode);

}

private function closeresource($resource)

{

fclose($resource);

}

public function savefile($filename)

{

$arr=$this->sort->sort();

$fopen=$this->getresource($filename,”a+”);

if(!$fopen){

echo “文件打开失败!”;exit;

}

var_dump($arr);

foreach($arr as $key=>$value)

{

fwrite($fopen,$value.”n”);

}

$this->closeresource($fopen);

}

public function readfile($filename)

{

$this->savefile($filename);

$fopen=$this->getresource($filename,”r”);

if(!$fopen){

echo “文件打开失败!”;exit;

}

$arr=array();

while(!feof($fopen))

{

$get=fgets($fopen);

if(!empty($get))

$arr[]=str_replace(“n”,””,$get);

}

$this->closeresource($fopen);

return $arr;

}

}

$file=new file(new sort(‘Apple Orange Banana Strawberry’),”E:\”);

$arr=$file->readfile(“fruit.dat”);

var_dump($arr);

?>

复制代码题四:

<?php

/**

* 单例模式,创建mysqli数据库链接的单例对象

* @author zhuwenqiong

*

*/

class Db

{

private static $instance;

public $handle;

private function __construct($host,$username,$password,$dbname)

{

$this->handle=NULL;

$this->getcon($host,$username,$password,$dbname);

}

public static function getBb()

{

self::$instance=new Db();

return self::$instance;

}

private function getcon($host,$username,$password,$dbname)

{

if($this->handle!=NULL){

return true;

}

$this->handle= mysqli_connect($host,$username,$password,$dbname);

}

}

复制代码最后,这些都是腾讯的面试题!不难吧,可当初就是习惯了代码提示(eclipse ide),导致很多函数只是记得有,但不敢确定到底是什么功能!

喵呜刷题:让学习像火箭一样快速,快来微信扫码,体验免费刷题服务,开启你的学习加速器!

创作类型:
原创

本文链接:PHP面试题详解 算法方面的

版权声明:本站点所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明文章出处。
分享文章
share