正则表达式笔试题php,2017年初级PHP程序员笔试题
2017年初級PHP程序員筆試題
6.實現一個字符串截取的函數,類似于substr,必須能夠截取中文這種多字節編碼。假設每個中文也是一個字符,普通的數字、符號、字母也是一個字
符。(提示:GB編碼的中文字符高位范圍是 x81-xFE )
function GBSubstr($str, $len){
$count = ;
for($i=; $i
if($count == $len) break;
if(preg_match("/[/x8-/xff]/", substr($str, $i, 1))) ++$i;
++$count;
}
return substr($str, , $i);
}
function GBSubstr2($src, $start=, $length=){
$suffix="";
$len = strlen($src);
if ( $len <= $length ) return $src;
$cut_length = ;
for( $idx = ; $idx
$char_value = ord($src[$idx]);
if ( $char_value < x8 || ( $char_value & x4 ) )
$cut_length++;
else
$cut_length = $cut_length + 3;
}
$curstr = substr($src, , $cut_length) ;
preg_match('/^([/x-/x7f]|.{3})*/', $curstr, $result);
return $result[];
}
function CSubstr($str, $start=, $length, $charset="gbk", $suffix=false){
if(function_exists("mb_substr")){
return mb_substr($str, $start, $length, $charset);
}
$re['utf-8'] = "/[/x1-/x7f]|[/xc2-/xdf][/x8-/xbf]|[/xe-/xef][/x8-/xbf]{2}|[/xf-/xff][/x8-/xbf]{3}/";
$re['gb2312'] = "/[/x1-/x7f]|[/xb-/xf7][/xa-/xfe]/";
$re['gbk'] = "/[/x1-/x7f]|[/x81-/xfe][/x4-/xfe]/";
$re['big5'] = "/[/x1-/x7f]|[/x81-/xfe]([/x4-/x7e]|/xa1-/xfe])/";
preg_match_all($re[$charset], $str, $match);
$slice = join("", array_slice($match[], $start, $length));
if($suffix) {
return $slice ."…";
}
return $slice;
}
7.寫一個遍歷指定目錄下所有子目錄和子文件的函數(提示:可以使用遞歸的方法)
function dir_all ( $path ) {
$handler = opendir($path);
while (false!==($tmp = readdir($handler))) {
if(is_dir( "$path/$tmp" )) {
if ($tmp=="." | $tmp=="..") continue;
echo $tmp."
/n";
dir_all ("$path/$tmp");
} else {
echo $tmp ."
/n";
}
}
}
8.寫出匹配郵箱地址和URL的兩個正則表達式。類似下面的:
郵箱地址:user_name.first@example.com.cn
URL地址:http://www.example.com.cn/user_profile.php?uid=1
(提示:使用標準的正則表達式,就是PHP中preg_* 類的正則處理函數能夠解析的正則)
郵箱://w+([-+.]/w+)*@/w+([-.]/w+)*/./w+([-.]/w+)*/
URL:/^http:[/w]+/.[/w]+[/S]*/
總結
以上是生活随笔為你收集整理的正则表达式笔试题php,2017年初级PHP程序员笔试题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 实验Matlab数值运算,MATLAB数
- 下一篇: php阿里的同步工具canal,基于阿里