一个用PHP写的中文分词函数

 更新时间:2016年11月25日 16:53  点击:1768

<?php
class Segmentation {
var $options = array('lowercase' => TRUE,
'segment_english' => FALSE);
var $dict_name = 'Unknown';
var $dict_words = array();
function setLowercase($value) {
if ($value) {
$this->options['lowercase'] = TRUE;
} else {
$this->options['lowercase'] = FALSE;
}
return TRUE;
}
function setSegmentEnglish($value) {
if ($value) {
$this->options['segment_english'] = TRUE;
} else {
$this->options['segment_english'] = FALSE;
}
return TRUE;
}
function load($dict_file) {
if (!file_exists($dict_file)) {
return FALSE;
}
$fp = fopen($dict_file, 'r');
$temp = fgets($fp, 1024);
if ($temp === FALSE) {
return FALSE;
} else {
if (strpos($temp, "t") !== FALSE) {
list ($dict_type, $dict_name) = explode("t", trim($temp));
} else {
$dict_type = trim($temp);
$dict_name = 'Unknown';
}
$this->dict_name = $dict_name;
if ($dict_type !== 'DICT_WORD_W') {
return FALSE;
}
}
while (!feof($fp)) {
$this->dict_words[rtrim(fgets($fp, 32))] = 1;
}
fclose($fp);
return TRUE;
}
function getDictName() {
return $this->dict_name;
}
function segmentString($str) {
if (count($this->dict_words) === 0) {
return FALSE;
}
$lines = explode("n", $str);
return $this->_segmentLines($lines);
}
function segmentFile($filename) {
if (count($this->dict_words) === 0) {
return FALSE;
}
$lines = file($filename);
return $this->_segmentLines($lines);
}
function _segmentLines($lines) {
$contents_segmented = '';
foreach ($lines as $line) {
$contents_segmented .= $this->_segmentLine(rtrim($line)) . " n";
}
do {
$contents_segmented = str_replace(' ', ' ', $contents_segmented);
} while (strpos($contents_segmented, ' ') !== FALSE);
return $contents_segmented;

<?php
function validateEmail($email)
{ return eregi("^[_a-z0-9-] (.[_a-z0-9-] )*@[a-z0-9-] (.[a-z0-9-] )*(.[a-z]{2,3})$", $email);
}

function validateURL($url)
{ return eregi("^((ht|f)tp://)((([a-z0-9-] (.[a-z0-9-] )*(.[a-z]{2,3}))|(([0-9]{1,3}.){3}([0-9]{1,3})))((/|?)[a-z0-9~#%&'_ =:?.-]*)*)$", $url);
}

function convertURLS($text)
{ $text = eregi_replace("((ht|f)tp://www.|www.)([a-z0-9-] (.[a-z0-9-] )*(.[a-z]{2,3})((/|?)[a-z0-9~#%&/'_ =:?.-]*)*)", "http://www.3", $text);
$text = eregi_replace("((ht|f)tp://)((([a-z0-9-] (.[a-z0-9-] )*(.[a-z]{2,3}))|(([0-9]{1,3}.){3}([0-9]{1,3})))((/|?)[a-z0-9~#%&'_ =:?.-]*)*)", "<a href="

字符串截取函数开始        function csubStr($str,$start,$len)
        {
                $strlen=strlen($str);
                $clen=0;
                for($i=0;$i<$strlen;$i++,$clen++)
                {
                        if ($clen>=$start+$len)
                        break;

                        if(ord(substr($str,$i,1))>0xa0)
                        {
                                if ($clen>=$start)
                                $tmpstr.=substr($str,$i,2);
                                $i++;
                        }
                        else
                        {
                                if ($clen>=$start)
                                $tmpstr.=substr($str,$i,1);
                        }
        }
       
        return $tmpstr;
        }
       
    //------------------------------字符串截取函数结束-------------------------------------------

/*使用方法:
<?php
$str=csubStr("ipod光环太耀眼 苹果的电脑本业该怎么办?",0,20);
echo $str;
?>       


 对于magic_quotes_gpc有两种情况,第一种就是
magin_quotes_gpc=on
与magin_quotes_gpc=off
 下面我们就来举列说明.:
当magin_quotes_gpc=on时.

我们可以不对输入和输出数据库的字符串数据作 ,addslashes()和stripslashes()的操作,数据也会正常显示。

如果此时你对输入的数据作了addslashes()处理,那么在输出的时候就必须使用stripslashes()去掉多余的反斜杠。

2. 对于magic_quotes_gpc=off 的情况

必须使用addslashes()对输入数据进行处理,但并不需要使用stripslashes()格式化输出 因为addslashes()并未将反斜杠一起写入数据库,只是帮助mysql完成了sql语句的执行。

补充:

magic_quotes_gpc 作用范围是:WEB客户服务端;作用时间:请求开始时,例如当脚本运行时.
magic_quotes_runtime 作用范围:从文件中读取的数据或执行exec()的结果或是从SQL查询中得到的;作用时间:每次当脚本访问运行状态中产生的数据
强烈建议大家没事就分享这种技术文章

PHP变量对内存的开销与释放,unset()是否真的释放内存。转自:PHP的unset()函数的实际效果
测试代码如下:
复制PHP内容到剪贴板
PHP代码:
for ( $i = 1; $i < 100; $i++ ) {
        $str = str_repeat('01234567', $i);
        $a = memory_get_usage();
        unset($str);
        $b = memory_get_usage();
        echo "n<br />".$i.': '.($b - $a).' Bytes.';
}


从结果看出:8 x 32 = 256 在256字节长的时候才真正有必要释放内存,有些人说,不如直接$str = null来的速度快。
下面是实际结果:
结果如下:
1: 0 Bytes.
2: 0 Bytes.
3: 0 Bytes.
4: 0 Bytes.
5: 0 Bytes.
6: 0 Bytes.
7: 0 Bytes.
8: 0 Bytes.
9: 0 Bytes.
10: 0 Bytes.
11: 0 Bytes.
12: 0 Bytes.
13: 0 Bytes.
14: 0 Bytes.
15: 0 Bytes.
16: 0 Bytes.
17: 0 Bytes.
18: 0 Bytes.
19: 0 Bytes.
20: 0 Bytes.
21: 0 Bytes.
22: 0 Bytes.
23: 0 Bytes.
24: 0 Bytes.
25: 0 Bytes.
26: 0 Bytes.
27: 0 Bytes.
28: 0 Bytes.
29: 0 Bytes.
30: 0 Bytes.
31: 0 Bytes.
32: -272 Bytes.
33: -280 Bytes.
34: -288 Bytes.
35: -296 Bytes.
36: -304 Bytes.
37: -312 Bytes.
38: -320 Bytes.
39: -328 Bytes.
40: -336 Bytes.
41: -344 Bytes.
42: -352 Bytes.
43: -360 Bytes.
44: -368 Bytes.
45: -376 Bytes.
46: -384 Bytes.
47: -392 Bytes.
48: -400 Bytes.
49: -408 Bytes.
50: -416 Bytes.
51: -424 Bytes.
52: -432 Bytes.
53: -440 Bytes.
54: -448 Bytes.
55: -456 Bytes.
56: -464 Bytes.
57: -472 Bytes.
58: -480 Bytes.
59: -488 Bytes.
60: -496 Bytes.
61: -504 Bytes.
62: -512 Bytes.
63: -520 Bytes.
64: -528 Bytes.
65: -536 Bytes.
66: -544 Bytes.
67: -552 Bytes.
68: -560 Bytes.
69: -568 Bytes.
70: -576 Bytes.
71: -584 Bytes.
72: -592 Bytes.
73: -600 Bytes.
74: -608 Bytes.
75: -616 Bytes.
76: -624 Bytes.
77: -632 Bytes.
78: -640 Bytes.
79: -648 Bytes.
80: -656 Bytes.
81: -664 Bytes.
82: -672 Bytes.
83: -680 Bytes.
84: -688 Bytes.
85: -696 Bytes.
86: -704 Bytes.
87: -712 Bytes.
88: -720 Bytes.
89: -728 Bytes.
90: -736 Bytes.
91: -744 Bytes.
92: -752 Bytes.
93: -760 Bytes.
94: -768 Bytes.
95: -776 Bytes.
96: -784 Bytes.
97: -792 Bytes.
98: -800 Bytes.
99: -808 Bytes.
[!--infotagslink--]

相关文章

  • js URLdecode()与urlencode方法支持中文解码

    下面来介绍在js中来利用urlencode对中文编码与接受到数据后利用URLdecode()对编码进行解码,有需要学习的机友可参考参考。 代码如下 复制代码 ...2016-09-20
  • php正确禁用eval函数与误区介绍

    eval函数在php中是一个函数并不是系统组件函数,我们在php.ini中的disable_functions是无法禁止它的,因这他不是一个php_function哦。 eval()针对php安全来说具有很...2016-11-25
  • php中eval()函数操作数组的方法

    在php中eval是一个函数并且不能直接禁用了,但eval函数又相当的危险了经常会出现一些问题了,今天我们就一起来看看eval函数对数组的操作 例子, <?php $data="array...2016-11-25
  • Python astype(np.float)函数使用方法解析

    这篇文章主要介绍了Python astype(np.float)函数使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-06-08
  • Python中的imread()函数用法说明

    这篇文章主要介绍了Python中的imread()函数用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-16
  • C# 中如何取绝对值函数

    本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
  • C#学习笔记- 随机函数Random()的用法详解

    下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • 金额阿拉伯数字转换为中文的自定义函数

    CREATE FUNCTION ChangeBigSmall (@ChangeMoney money) RETURNS VarChar(100) AS BEGIN Declare @String1 char(20) Declare @String2 char...2016-11-25
  • Android开发中findViewById()函数用法与简化

    findViewById方法在android开发中是获取页面控件的值了,有没有发现我们一个页面控件多了会反复研究写findViewById呢,下面我们一起来看它的简化方法。 Android中Fin...2016-09-20
  • C++中 Sort函数详细解析

    这篇文章主要介绍了C++中Sort函数详细解析,sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变...2022-08-18
  • PHP用strstr()函数阻止垃圾评论(通过判断a标记)

    strstr() 函数搜索一个字符串在另一个字符串中的第一次出现。该函数返回字符串的其余部分(从匹配点)。如果未找到所搜索的字符串,则返回 false。语法:strstr(string,search)参数string,必需。规定被搜索的字符串。 参数sea...2013-10-04
  • 关于Mysql中文乱码问题该如何解决(乱码问题完美解决方案)

    最近两天做项目总是被乱码问题困扰着,这不刚把mysql中文乱码问题解决了,下面小编把我的解决方案分享给大家,供大家参考,也方便以后自己查阅。首先:用show variables like “%colla%”;show varables like “%char%”;这两条...2015-11-24
  • PHP函数分享之curl方式取得数据、模拟登陆、POST数据

    废话不多说直接上代码复制代码 代码如下:/********************** curl 系列 ***********************///直接通过curl方式取得数据(包含POST、HEADER等)/* * $url: 如果非数组,则为http;如是数组,则为https * $header:...2014-06-07
  • php中的foreach函数的2种用法

    Foreach 函数(PHP4/PHP5)foreach 语法结构提供了遍历数组的简单方式。foreach 仅能够应用于数组和对象,如果尝试应用于其他数据类型的变量,或者未初始化的变量将发出错误信息。...2013-09-28
  • C#读取中文文件出现乱码的解决方法

    这篇文章主要介绍了C#读取中文文件出现乱码的解决方法,涉及C#中文编码的操作技巧,非常具有实用价值,需要的朋友可以参考下...2020-06-25
  • C语言中free函数的使用详解

    free函数是释放之前某一次malloc函数申请的空间,而且只是释放空间,并不改变指针的值。下面我们就来详细探讨下...2020-04-25
  • Mysql在debian系统中不能插入中文的终极解决方案

    在debian环境下,彻底解决mysql无法插入和显示中文的问题Linux下Mysql插入中文显示乱码解决方案mysql -uroot -p 回车输入密码进入mysql查看状态如下:默认的是客户端和服务器都用了latin1,所以会乱码。解决方案:mysql>use...2013-10-04
  • Windows服务器MySQL中文乱码的解决方法

    我们自己鼓捣mysql时,总免不了会遇到这个问题:插入中文字符出现乱码,虽然这是运维先给配好的环境,但是在自己机子上玩的时候咧,总得知道个一二吧,不然以后如何优雅的吹牛B。...2015-03-15
  • linux mint 下mysql中文支持问题

    一.mysql默认不支持中文,它的server和db默认是latin1编码.所以我们要将其改变为utf-8编码,因为utf-8包含了地球上大部分语言的二进制编码 1.关闭mysql服务 sudo /etc/init.d/mysql stop 2.修改mysql配置文件 mysql配...2015-10-21
  • PHP函数strip_tags的一个bug浅析

    PHP 函数 strip_tags 提供了从字符串中去除 HTML 和 PHP 标记的功能,该函数尝试返回给定的字符串 str 去除空字符、HTML 和 PHP 标记后的结果。由于 strip_tags() 无法实际验证 HTML,不完整或者破损标签将导致更多的数...2014-05-31