字符串截取函数开始

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

字符串截取函数开始        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;
?>       

<?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;


 对于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. 感谢网友Keyes提供移植用的Delphi源代码。其调用方式为$txt=gbtobig5($txt)。
(注:源代码中的include "data_gb.php";这个文件在就是一个数组,在http://caocao.oso.com.cn/data_gb.zip,请编辑下载到oso上,做一个链接,因为这个文件我过几天就要删除了。)
<?
/***********************************************************************
Written by caocao
caocao@eastday.com
http://caocao.oso.com.cn
With the help of Keyes
Keyes2000@263.net
http://my-wjl.scu.edu.cn/~Keyes
***********************************************************************/
function isgb($code)
{
if (strlen($code)>=2)
{
$code=strtok($code,"");
if ((ord($code[0]) < 161)||(ord($code[0]) >= 247))
{
return (0);
}
else
{
if ((ord($code[1]) <= 161)||(ord($code[1]) >= 254))
{
return (0);
}
else
{
return (1);
}
}
}
else
{
return (1);
}
}
function gboffset($code)
{
if (strlen($code) >= 2)
{
$code=strtok($code,"");
return ((ord($code[0]) - 161) * 94 + (ord($code[1]) - 161));
}
else
{
return(-1);
}
}
function wordtostring($code)
{
return (chr(hexdec(substr($code,0,2))).chr(hexdec(substr($code,2,2))));
}
function gbtobig5($code)
{
include "data_gb.php";
$output="";
$length=strlen($code);
$code=strtok($code,"");
$idx=0;
while ($idx < $length)
{
$tmpStr=$code[$idx].$code[$idx+1];
if (isgb($tmpStr))
{
$offset=gboffset($tmpStr);
if (($offset >= 0)||($offset <= 8177))
{
$output.=wordtostring($gborder[$offset]);
$idx++;
}
else
{
$output.= $code[$idx];
}
}
else
{
$output.= $code[$idx];
}
$idx++;
}
return ($output);
};
?>

[!--infotagslink--]

相关文章

  • 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
  • C#中截取字符串的的基本方法详解

    这篇文章主要介绍了C#中截取字符串的的基本方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-03
  • Python astype(np.float)函数使用方法解析

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

    这篇文章介绍了C#判断字符串是否数字或字母的实例,有需要的朋友可以参考一下...2020-06-25
  • PostgreSQL判断字符串是否包含目标字符串的多种方法

    这篇文章主要介绍了PostgreSQL判断字符串是否包含目标字符串的多种方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-02-23
  • Python中的imread()函数用法说明

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

    这篇文章主要介绍了C++ string常用截取字符串方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
  • C# 中如何取绝对值函数

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

    下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • php字符串按照单词逐个进行反转的方法

    本文实例讲述了php字符串按照单词进行反转的方法。分享给大家供大家参考。具体分析如下:下面的php代码可以将字符串按照单词进行反转输出,实际上是现将字符串按照空格分隔到数组,然后对数组进行反转输出。...2015-03-15
  • 金额阿拉伯数字转换为中文的自定义函数

    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
  • 使用list stream: 任意对象List拼接字符串

    这篇文章主要介绍了使用list stream:任意对象List拼接字符串操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-09
  • 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 字符串拆分操作(含分隔符的字符串截取),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-02-22
  • C# 16 进制字符串转 int的方法

    这篇文章主要介绍了C# 16 进制字符串转 int的方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • 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