替换字符串

类:
<?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","***");
?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值