QQ腾讯php面试题--腾讯php算法题目
腾讯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());


腾讯php面试题题二:

/**
* 对于用户输入一串字符串$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面试题题三:

<?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 funct
相关的问题: 要查看答案或进行讨论,请点击进入相关链接:
友情链接IT题库(原一题库)(ittiku.com)| 版权归yishouce.com所有| 友链等可联系 admin#yishouce.com