![]() |
|
首页 │ Apache │ Linux│ Java│ MySQL│ 注册│帮助 | |||
这道题是我去面试时经理给我做的机试题,我现在已经获聘,但答卷还有些问题没有完成,有兴趣的讨论一下:
题目:编写一个字串处理类,功能要求如下
1 查出指定字串(一个或多个,下同)是否在文中出现,并返回出现次数
2 指定字词高亮显示
3 过滤指定字词(替换成指定字符,如***)
4 去除文中的不安全的HTM标记,但保留指定的单字标志(如:b,i,u等)
5 文中的URL转为可点击(指定哉才可点,其它出掉)
6 UBBCode解析
7 为该类编写testcase
我的答案如下:
[php]
<?php
/**
文件名 : strparse.php
功能 : 字符串处理
作者 : tim
联系 : timwhoung@126.com
完成日期: 2004-11-24
*/
class strparse
{
var $words; //指定字词出现次数统计结果
var $str; //处理内容,为一字串
//构造
function strparse($str){
$this->str=$str;
}
/**
功能:返回指定字串的统计结果,为一数组(键名为要查的词,值为出现次数)
参数:传入参数为一数组,保存要统计的字串
*/
function getWords($wordsArr){
return $this->words;
}
//设置要处理的字串
function setStr($str){
$this->str=$str;
}
//获取处理后内容
function getStr(){
return nl2br($this->str);
}
/**
功能:统计指定字串出现次数
参数
urlArr要统计的字串数组
*/
function countWords($keywordArr){
if(is_array($keywordArr)){
foreach($keywordArr as $k=>$v){
$this->words[$v]=substr_count($this->str,$v);
}
}
return $this->words;
}
/**
功能:高亮指定关键字
参数
hightArr高亮关键字数组,$s_code和$e_code为高亮处理方式
*/
function setH($hightArr,$s_code="",$e_code=""){
if(is_array($hightArr)){
foreach($hightArr as $k=>$v){
$this->str=str_replace($v,$s_code.$v.$e_code,$this->str);
}
}
}
/**
功能:过滤指定关键字
参数
filterArr高亮关键字数组,$mask替代字串
*/
function filter($filterArr,$mask="***"){
if(is_array($filterArr)){
foreach($filterArr as $k=>$v) $f="/".$v."/A";//对每个关键字添加定界符及模式修正符A
}
$this->str=preg_replace($f,$mask,$this->str);
}
/**
功能:去除不安全的HTML字符
参数
tag要保留的标记
*/
function htmlClean($tag=""){
$this->str=strip_tags($this->str,$tag);
}
/**
功能:自动转换指定链接,并去除多余项
参数
urlArr要转换的url数组
*/
function urlClean($urlArr){
//保护保留URL
if(is_array($urlArr)){
foreach($urlArr as $k=>$v){
$this->str=str_replace($v,"[url]".($k)."[/url]",$this->str);
if(substr($v,0,7)!="http://") $v="http://".$v;
$url[$k]=$v;
}
}
//保护UBB的[IMG]和[URL]中的URL
preg_match_all("/\[img(=left|=right)?\](.+?)\[\/img\]/is",$this->str,$result);
foreach($result[0] as $k=>$v){
$this->str=str_replace($v,"ubbimg".$k,$this->str);
$ubbimg[$k]=$v;
}
preg_match_all("/\[url=(.+?)\](.+?)\[\/img\]/is",$this->str,$result);
foreach($result[0] as $k=>$v){
$this->str=str_replace($v,"ubburl".$k,$this->str);
$ubburl[$k]=$v;
}
//去掉其它URL
$this->str=eregi_replace("(http:\/\/)?(www\.)?([0-9a-zA-Z_\-\.]{2,})?\.(com|net|org|cn|gov)","",$this->str);
//恢复保留URL
if(is_array($url)){
foreach($url as $k=>$v){
$this->str=str_replace("[url]".$k."[/url]","".$v."",$this->str);
}
}
//还原ubb编码的url
if(is_array($ubburl)){
foreach($ubburl as $k=>$v){
$this->str=str_replace("ubburl".$k,$v,$this->str);
}
}
if(is_array($ubbimg)){
foreach($ubbimg as $k=>$v){
$this->str=str_replace("ubbimg".$k,$v,$this->str);
}
}
}
/**
*功能:UBB编码
*/
function BBCODE(){
$this->str=str_replace(" "," ",$this->str);
$this->str=preg_replace("/\\t/is"," ",$this->str);
$this->str=preg_replace("/\(.+?)\[\/color\]/is","\\2",$this->str);
$this->str=preg_replace("/\[url=(http:\/\/.+?)\](.+?)\[\/url\]/is","\\2",$this->str);
$this->str=preg_replace("/\[url=(.+?)\](.+?)\[\/url\]/is","\\2",$this->str);
$this->str=preg_replace("/\[img(=left|=right)?\](.+?)\[\/img\]/is","screen.width-300)this.width=screen.width-300\" title=\"按此在新窗口浏览图片\">",$this->str);
$this->str=preg_replace("/\[i\](.+?)\[\/i\]/is","\\1",$this->str);
$this->str=preg_replace("/\[u\](.+?)\[\/u\]/is","\\1",$this->str);
$this->str=preg_replace("/\[b\](.+?)\[\/b\]/is","\\1",$this->str);
$this->str=preg_replace("/\[wmv\](.+?)\s*\[\/wmv\]/is","",$this->str);
$this->str=preg_replace("/\[wma\](.+?)\s*\[\/wma\]/is","",$this->str);
$this->str=preg_replace("/\[mp3\](.+?)\s*\[\/mp3\]/is",'',$this->str);
$this->str=preg_replace("/\[swf\](.+?)\s*\[\/swf\]/is","[在新窗口中查看该Flash]
",$this->str);
$this->str=preg_replace("/\[quote\](.+?)\[\/quote\]/is"," 引用: \\1
", $this->str);
$this->str=preg_replace("/\[code\]\s*(.*?)\s*\[\/code\]/is"," CODE: \\1
", $this->str);
}
}
?>
[/php]
testcase.php(该例子经主考官修改)
[php]
<?php
//字串处理类测试例子
require_once("strparse.php");
$ss=<<
");
$hight=array("本田","张广宁");//要高亮显示的词
$str->setH($hight);
$filter=array("屎","朱云珊","本报讯");//要过滤的词
$str->filter($filter);
/*
jason@2004/11/25 11:45 好像去了一个gz.dayoo.com, 但有一个图片的gz.dayoo.com却无去?! 应该可以:指定dayoo.com, 那么其子域的也可以保留.
*/
$url=array("http://www.dayoo.com");//要去掉的URL
$str->urlClean($url);//去掉非指定的URL
$str->BBCODE();
echo($str->getStr());
?>[/php]
存在的问题:
1 在替换过滤中文文字时,某些词会出现乱码,后来我加上了模式修正符A解决了该问题,不知道为什么会这样?
2 某些安全的HTM标记(如注释,书名号<<>>)也会被去掉,目前我的想法也是先将这些标记保护起来(替换成非HTM),处理完后才还原
3 保留和去除URL的问题,自已认为效率较低,而且还不完善
请大家对以上程序和想法,各抒己见!

