您的位置:首页 > 其它

替换字符串

2006-05-17 21:22 197 查看
类:
<?php
/**
* @ Purpose: 字符串替换类,把给定的字符串中的某些特定字符转换成需要的字符
* @ Package Name: filtrate
* @ Author: Allan
* @ E-Mail: Allan8212@163.com
* @ Date Created: 2006-5-17
*/
class filtrate
{
var $string;

/**
* @ Purpose: 构造函数
* @ Method Name: _construct()
* @ Parameter: string $string 需要替换的字符串
* @ Return: void
*/
function __construct($string)
{
$this->string = iconv("GB2312","UTF-8",$string);
}

/**
* @ Purpose: 替换需要的内容,用$key替换$array的内容
* @ Method Name: replace()
* @ Parameter: string $array 替换的内容(需要替换多个内容时用英文|隔开);string $key 替换后的内容
* @ Return: $this->string 返回处理完毕后的字符串
*/
function replace($array,$key)
{
$array = iconv("GB2312","UTF-8",$array);
$key = iconv("GB2312","UTF-8",$key);
$this->string = preg_replace("(".$array.")",$key,$this->string);
return iconv("UTF-8","GB2312",$this->string);
}
}

调用:
<?php
include("filtrate.class.php");
$file=fopen("http://news.sina.com.cn/w/2006-05-17/05198944562s.shtml","r");

//put the text inside the file in the XML object variable
while (!feof($file))
{
$fileString.=fread($file,4096);
}

//close the opened file
fclose($file);
$myreplace = new filtrate($fileString);
echo $myreplace->replace("美国|五角大楼|911","***");
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: