php去除重复字符的实现代码

PHP 1485 0 2013-06-28

php去除重复字符的实现代码
方法一:
$text = '数组aabbccdd';
$text_filter = '';
$filter = array();
$len = mb_strlen($text, 'utf-8');
for ($i = 0; $i<$len; $i++) {
$char = mb_substr($text, $i, 1, 'utf-8');
if (!isset($filter[$char])) {
$text_filter .= $char;
$filter[$char] = $char;
}
}
echo $text_filter;

方法二:
$string= '数组aabbccdd';
function str_split_utf8($str) {
$split=1;
$array = array();
for ( $i=0; $i < strlen( $str ); ){
$value = ord($str[$i]);
if($value > 127){
if($value >= 192 && $value <= 223)
$split=2;
elseif($value >= 224 && $value <= 239)
$split=3;
elseif($value >= 240 && $value <= 247)
$split=4;
}else{
$split=1;
}
$key = NULL;
for ( $j = 0; $j < $split; $j++, $i++ ) {
$key .= $str[$i];
}
array_push( $array, $key );
}
return $array;
}
print_r(array_unique(str_split_utf8($string)));

方法三:
就是把每一个字分割在数组里再用array_unique()这个函数。

上一篇:PHP-cookie详解,Cookie工作原理,Cookie注意事项

下一篇:php 去掉完全相同的重复数组

讨论数量:1

天涯网魂 3 杠 5 星2013-06-28 23:35:58

php简单去除大型文本重复
<?php
error_reporting(0);
@ini_set('memory_limit','-1');
set_time_limit(0);
echo" 去除文本重复工具"."\r\n\r\n";
echo"\n"."输入要整理的文件:"."\n";
$dic=trim(fgets(STDIN));
/*while (!feof($dic)){
        $file[]=stream_get_line($fp,65535,"\r\n");
}
 */
 
$file=file($dic);
$array=preg_replace('/($\s*$)|(^\s*^)/m','',$file); //消除空行
$new_array=array_values(array_unique($array));//消除重复行
$new_filename="new_". basename($dic);
 
if(file_put_contents("$new_filename",join("\r\n",$new_array))){
 
        $num=count($file);
        $new_num=count($new_array);
        $counts=$num-$new_num;
        $files=dirname(__FILE__).DIRECTORY_SEPARATOR.$new_filename;
 
echo<<<INFO
 +----------------------------------------------+
 | [+] 去除重复完毕!   www.2cto.com            |
 | [+] 整理后的文件为:$files     |
 | [+] 原始字典数量:$num 行                     |
 | [+] 整理后为:$new_num 行                         |
 | [+] 共替换了$counts 行                           |
 +----------------------------------------------+
INFO;
 
}
 
else{ 
        echo"------------------------------------------"."\r\n";
        echo"[*] 错误!"."\r\n\r\n";
        echo"[*] 找不到文件!请检查输入路径是否存在!"."\r\n";
        echo"------------------------------------------"."\r\n";
exit();
}
?> 

请先登录再发表讨论。 2024-04-27

天涯网魂
3 杠 5 星
TA 的文章
TA 的随言
TA 的资源链