替换字符串
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","***");
?>
<?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","***");
?>
相关文章推荐
- linux 下常见的文件内字符串替换命令
- 如何替换一段字符串中的文字
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- 字符串替换(南阳113)
- 【面试算法系列】替换字符串中的空格为其他字符 - C语言实现
- 字符串替换命令 sed
- java字符串的替换replace、replaceAll、replaceFirst的区别详解
- Gvim应用命令(字符串替换查找)
- C语言字符串查找替换
- JavaScript使用Replace进行字符串替换的方法
- linux shell 字符串操作(长度,查找,替换)详解
- Java IO浅谈(个人感觉例子不错,替换流,数组,文件或者大的字符串中的一些字符或者字符串)
- linux shell 字符串操作(长度,查找,替换)详解
- IPHONE 开发 7 -- Object C 02 字符串NSString 与 char* ,字符串的遍历,字符串的比较,截取与大小写改变,搜索字符串与替换字符串
- 面试之路(20)-字符串中空格替换一类问题详解
- 给定一个字典,通过查找这个字典,替换给定的字符串中的中文为英文
- PHP开发之替换字符串实现图片自适应屏幕
- 批量替换字符串
- Javascript 字符串替换
- Delphi的StringReplace 字符串替换函数