imd函数sql截取字符串不同长度字符串的末位3位数

页面导航:
→ 正文内容 substr 截取字符串 乱码
PHP substr 截取字符串出现乱码问题解决方法[utf8与gb2312]
在PHP中,使substr函数截取字符串末位会出现乱码,因为中文UTF-8编码,每个汉字占3字节,而GB2312占2字节,英文占1字节,截取位不准确,造成断开的字符会把其后的..拉过来一起做一个字,所以出现了乱码。
substr --- 取得部份字符串 语法 : string substr (string string, int start [, int length]) 说明 : substr( )传回 string的一部份字符串,由参数 start和 length指定。 如果 start是正数,传回的字符串将会从 string的第 start个字元开始。 Example :
代码如下: &?php $rest = substr ("abcdef", 1); // returns "bcdef" $rest = substr ("abcdef", 1, 3); // returns "bcd" ?&
如果 start是负数,传回的字符串将会从 string结尾的第 start个字开始。 Example :
代码如下: &?php $rest = substr ("abcdef", -1); // returns "f" $rest = substr ("abcdef", -2); // returns "ef" $rest = substr ("abcdef", -3, 1); // returns "d" ?&
如果有给予参数 length而且是正数时,传回的字符串将会从 start传回 length个字元。 如果有给予参数 length而且是负数时,传回的字符串将会结束于 string结尾的第 length个字元。 Example :
代码如下: &?php $rest = substr ("abcdef", 1, -1); // returns "bcde" ?&
dongyue, 11:10:41 substr --- 取得部份字符串 语法 : string substr (string string, int start [, int length]) 说明 : substr( )传回 string的一部份字符串,由参数 start和 length指定。 如果 start是正数,传回的字符串将会从 string的第 start个字元开始。 Example :
代码如下: &?php $rest = substr ("abcdef", 1); // returns "bcdef" $rest = substr ("abcdef", 1, 3); // returns "bcd" ?&
如果 start是负数,传回的字符串将会从 string结尾的第 start个字开始。 Example :
代码如下: &?php $rest = substr ("abcdef", -1); // returns "f" $rest = substr ("abcdef", -2); // returns "ef" $rest = substr ("abcdef", -3, 1); // returns "d" ?&
如果有给予参数 length而且是正数时,传回的字符串将会从 start传回 length个字元。 如果有给予参数 length而且是负数时,传回的字符串将会结束于 string结尾的第 length个字元。 Example :
代码如下: &?php $rest = substr ("abcdef", 1, -1); // returns "bcde" ?&
Utf-8、gb2312都支持的汉字截取函数
代码如下: //截取中文字符串 /* Utf-8、gb2312都支持的汉字截取函数 cut_str(字符串, 截取长度, 开始长度, 编码); 编码默认为 utf-8 开始长度默认为 0 */function cut_str($string, $sublen, $start = 0, $code = 'UTF-8') { if($code == 'UTF-8') { $pa ="/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/"; preg_match_all($pa, $string, $t_string); if(count($t_string[0]) - $start & $sublen) return join('', array_slice($t_string[0], $start, $sublen)); return join('', array_slice($t_string[0], $start, $sublen)); } else { $start = $start*2; $sublen = $sublen*2; $strlen = strlen($string); $tmpstr = ''; for($i=0; $i&$ $i++) { if($i&=$start && $i&($start+$sublen)) { if(ord(substr($string, $i, 1))&129) { $tmpstr.= substr($string, $i, 2); } else { $tmpstr.= substr($string, $i, 1); } } if(ord(substr($string, $i, 1))&129) $i++; } if(strlen($tmpstr)&$strlen ) $tmpstr.= ""; return $ } } $str="脚本之家一个不错的网站"; echo cut_str($str, 8, 5, 'gb2312');
您可能感兴趣的文章:
上一篇:下一篇:
最 近 更 新
热 点 排 行
12345678910用心创造滤镜
扫码下载App
汇聚2000万达人的兴趣社区下载即送20张免费照片冲印
扫码下载App
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
阳光总在风雨后,乌云上有晴空,珍惜所有的感动,每一份希望都在你手中......【助你成功】圈子QQ千人群: 4204558
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
奋斗推荐阅读:
& 10:37:18
& 15:08:51
& 13:01:21
& 09:23:00
& 21:35:02
阅读(427)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
loftPermalink:'',
id:'fks_',
blogTitle:'php替换字符串函数strtr()和str_repalce()区别',
blogAbstract:'
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}c语言 字符串替换函数_百度知道
c语言 字符串替换函数
  替换函数主要有strtr(),str_repalce()这两个函数。  首先针对strtr函数第一种方式:  我们看看下面的举例:  &?php
echo strtr(&I Love you&,&Lo&,&lO&);
?&  得到的结果是:  I lOve yOu  这个结果提醒我们:  1.strtr它是区分大小写的  2.strtr的替换是很特殊的,你注意看后面那个yOu,中间的O被替换的,这显然不是我们的本意。再举一个特殊例子,说明这个php的sttr函数的怪异  &?phpecho strtr(&I Love you&,&Love&,&&);?&  结果是:  I Love you  什么也不会改变,所以strtr需要注意的是:  3.不能被替换为空,也就是末位那个参数不能是空字符串,当然空格是可以的。  再次举例strtr函数的另一种情况:  &?phpecho strtr(&I Loves you&,&Love&,&lOvEA&);?&  结果是:  I lOvEs yOu  注意看第三个参数的A,在结果中并没有出现。  4.我不建议用strtr以少换多。  ok,既然这个strtr函数挺麻烦为什么还要用呢?  原因是,它的速度很快。据说,strtr 比 str_replace 快四倍。  5.能用strtr函数的时候一定要用。  第二种情况:  strtr(string,array)  6.strtr符合意愿的使用方法  &?php$table_change = array('you'=&'her sister');echo strtr(&I Love you&,$table_change);?&  结果为:  I Love her sister  7.小技巧:你想到替换什么你就往数组加什么  比如:  &?php$table_change = array('you'=&'her sister');$table_change += array('Love' =& 'hate');echo strtr(&I Love you&,$table_change);?&  结果是:  I hate her sister  再次提醒那个Love 写成love 是行不通的哦。  字符串取代。  语法: string str_replace(string needle, string str, string haystack);  返回值: 字符串  函数种类: 资料处理  内容说明  本函数将字符串 str 代入 haystack 字符串中,将所有的 needle 置换成 str。下例将 %body% 以 black 取代  &?php$bodytag = str_replace(&%body%&, &black&, &&body text=%body%&&);echo $?&  格式:  [@str_replace(&要替换的旧内容&, &要取代原内容的新字符&, $被替换内容的变量名)][@str_replace(array('旧1','旧2','旧3'), array('新1','新2','新3'), $被替换内容的变量名)][@str_replace(array('旧1','旧2','旧3'), '新内容', $被替换内容的变量名)]   实例:  多对一替换:想把内容字段里所有的&p&&/p&标签清除掉,替换成空 [@str_replace(array('&p&','&/p&'), '', $Content)]  一对一替换:想把内容字段里所有的&br&标签换成&p& [@str_replace('&br&', '&p&', $Content)]  多对多替换:想把内容字段里的&br&换成&br /&, 同时&p&换&hr&,把&/p&全清除 [@str_replace(array('&br&', '&p&','&/p&'), array('&br /&','&hr&',''), $Content)]    
其他类似问题
为您推荐:
提问者采纳
直接 用strcmp()函数每次 取 按要求 的 长度
替换 替换 后 放在 一个新的
数组 里 ,OK
提问者评价
字符串的相关知识
其他10条回答
这个说法本身就不科学,字符串操作本身就依赖指针,字符串的脚标也是偏移指针,什么都不用,还替换个P哦。
等不用指针的答案
那你只有先把字符串专换成字符类型了来,再用字符数组来解决
向别人请教问题还这么凶!没有教养!
这个简单啊.不过对数组的操作还是通过指针实现的.楼主对C的了解还处于未入门阶段.
呵呵,楼主 是初学?
字符串本身就是指针形式,除非你定义为字符数组。不懂还这么拽,楼主真有你的,呵呵。
用类库中的函数,有很多,你可以联合使用。可以查以下,相信你一定会做,因为凶神没有不会做的!
字符串 ~ 首地址
~怎么说都要对地址进行操作,也许表面没用指针,但实际是一样的!
平时我一般是很懒的,但是今天为了楼主出的60分我勤奋了一把,楼主看看我写的怎么样!
#include &stdio.h&
#include &string.h&
#define TRUE 1
#define FALSE 0
void tihuan(char []);
void main()
char str[100];
gets(str);
tihuan(str);
void tihuan(char str[])
int panduan=FALSE;
int leng=0;
leng=strlen(str)+1;
while(*(str+i)!='\0')
if(*(str+i)=='b'&&*(str+(i+1))=='c')
panduan=TRUE;
if(panduan)
*(str+i)='z';
leng=strlen(str)+1;
panduan=FALSE;
for(j=i+1;j&j++)
*(str+j)=*(str+(j+1));
*(str+j)='\0';
printf(&%...
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁当前访客身份:游客 [
当前位置:
发布于 日 16时,
AVR-GCC平台里面没有浮点转字符串
我写了个。特别的地方是末位舍入用随机舍入法
代码片段(1)
1.&[代码][C/C++]代码&&&&
&Floating number to string&
Copyright (C) &2012&
&fss.sosei&
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program.
If not, see &http://www.gnu.org/licenses/&.
double random_round (double x, int_fast8_t rounded_to_exp10)
const uint_fast8_t MAX_SIGNIFICANT_FIGURES = 5;
//float floor(log10(pow(2, 23)))-1, double floor(log10(pow(2, 52)))-1
if (not isfinite(x))
double absolute_value = fabs(x);
if ((floor(log10(absolute_value)) - rounded_to_exp10) &= MAX_SIGNIFICANT_FIGURES)
double integer_part, decimal_
decimal_part = modf(absolute_value * pow(10, -rounded_to_exp10), &integer_part);
if(decimal_part & 0.5)
integer_part += 1;
if(decimal_part == 0.5)
integer_part += rand() % 2;
return copysign((integer_part + 0.1) * pow(10, rounded_to_exp10), x);
uint_fast8_t floating_number_to_string (double input_number, uint_fast8_t decimal_digits, char numeric_string[], uint_fast8_t array_len)
//input_number range is 99999 to 0.000001. Returns the numeric string length. The random roundoff, pay attention to the random number seed.
const uint_fast8_t MAX_SIGNIFICANT_FIGURES = 5;
//float floor(log10(2^23))-1
const uint_fast8_t MIN_DECIMAL = 6;
char temp_string[10];
//a sign + (decimal_digits == 0 ? MAX_SIGNIFICANT_FIGURES : max((MAX_SIGNIFICANT_FIGURES + a decimal), (a zero + a decimal + MIN_DECIMAL))) + '\x0'
double absolute_
double num_exp10;
if (not isfinite(input_number))
if (decimal_digits & MIN_DECIMAL)
absolute_value = fabs(input_number);
num_exp10 = floor(log10(absolute_value));
if (num_exp10 &= MAX_SIGNIFICANT_FIGURES)
//Value ultra range error
double num_tail_exp10 = fmax((num_exp10 + 1 - MAX_SIGNIFICANT_FIGURES), (-((int_fast8_t)decimal_digits)));
//Plus integer part
input_number = random_round(absolute_value, lround(num_tail_exp10));
absolute_value = fabs(input_number);
num_exp10 = floor(log10(absolute_value));
int_fast8_t num_int_exp10 = lround(num_exp10);
if (num_int_exp10 &= MAX_SIGNIFICANT_FIGURES)
//Value ultra range error
num_tail_exp10 = fmax((num_exp10 + 1 - MAX_SIGNIFICANT_FIGURES), (-((int_fast8_t)decimal_digits)));
//Plus integer part
int_fast8_t num_int_tail_exp10 = lround(num_tail_exp10);
if (num_int_tail_exp10 & num_int_exp10)
num_exp10 = num_tail_exp10;
num_int_exp10 = num_int_tail_exp10;
uint_fast8_t num_count = num_int_exp10 - num_int_tail_exp10 + 1;
double intermediate_number = absolute_value * pow(10, -num_exp10);
double digital_character[num_count];
for (int_fast8_t i = 0; i & num_ ++i)
intermediate_number = modf(intermediate_number, &digital_character[i]) * 10;
int_fast8_t i = 0;
uint_fast8_
if ((input_number & 0 ? 1 : 0) == 1)
temp_string[i] = '-'; ++i;
int_fast8_t j = 0;
if (num_int_exp10 &= 0)
n = i + num_int_exp10;
while(i &= n)
temp_string[i] = (char)(lround(digital_character[j]) + 0x30);
//0x30 == '0'
if ((num_int_tail_exp10 & 0 ? 1 : 0) == 1)
temp_string[i] = '.'; ++i;
n = i + abs(num_int_tail_exp10);
while(i & n)
temp_string[i] = (char)(lround(digital_character[j]) + 0x30);
//0x30 == '0'
temp_string[i] = '0'; ++i;
temp_string[i] = '.'; ++i;
n = i + (-1 - num_int_exp10);
while(i & n)
temp_string[i] = '0'; ++i;
n = i + (num_int_exp10 - num_int_tail_exp10);
while(i &= n)
temp_string[i] = (char)(lround(digital_character[j]) + 0x30);
//0x30 == '0'
temp_string[i] = '\x0';
if ((n + 1) & array_len)
for (i=0; i &= ++i)
numeric_string[i] = temp_string[i];
//Returns the numeric string length
开源中国-程序员在线工具:
开源从代码分享开始
fsssosei的其他代码}

我要回帖

更多关于 sql截取字符串 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信