PHP判断字符串编码是否为utf8以及转换问题

  今天说说编码乱码问题,当一个页面存在两种编码的时候,无论你乍么选择都会出现乱码,解决方法:

  1.你据在网页编码是哪个。

  2.首先判断字符串编码是否为utf8代码如下:

  PHP代码

  function is_utf8($word)

  {

  if (preg_match("/^([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}/",$word) == true || preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}$/",$word) == true || preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){2,}/",$word) == true)

  {

  return true;

  }

  else

  {

  return false;

  }

  }

  3.再转换为gb2312,用到转换函数iconv;

  PHP代码

  if(is_utf8($tit)==1)

  $tit = iconv("utf-8","gbk",$tit);

五月份投资者们做点什么【带鱼投资理财】:http://licai.daiyuline.com/caijingxinwen/335.html

5月开盘大吉 沪指上涨1.85%【证券投资】:http://licai.daiyuline.com/zhengquan/334.html

时间: 2024-08-29 12:15:10

PHP判断字符串编码是否为utf8以及转换问题的相关文章

c++字符串编码GBK到UTF8的转换

使用c++跨windows和linux平台实现字符串GBK到UTF8的转换. 原理是GBK字符串先转为unicode编码,然后再转换为UTF8编码. 代码如下: #ifndef __CODE_CONVERT_H__ #define __CODE_CONVERT_H__ #include <cstdio> #include <stdlib.h> #include <locale.h> #include <string> #if defined(_WIN32)

Python判断字符串编码以及编码的转换

判断字符串编码 使用 chardet 可以很方便的实现字符串/文件的编码检测.尤其是中文网页,有的页面使用GBK/GB2312,有的使用UTF8,如果你需要去爬一些页面,知道网页编码很重要 >>> import urllib >>> html = urllib.urlopen('http://www.chinaunix.net').read() >>> import chardet >>> chardet.detect(html) {

python_判断字符串编码的方法

1. 安装chardet 在命令行中,进入Python27\Scripts目录,输入以下的命令:easy_install chardet 2. 操作 import chardet f = open('file','r') fencoding=chardet.detect(f.read()) print fencoding fencoding输出格式 {'confidence': 0.96630842899499614, 'encoding': 'GB2312'} ,只能判断是否为某种编码的概率.

php转换字符串编码 iconv与mb_convert_encoding的区别

PHP判断字符串编码函数mb_detect_encoding总结 iconv — Convert string to requested character encoding(PHP 4 >= 4.0.5, PHP 5) mb_convert_encoding — Convert character encoding(PHP 4 >= 4.0.6, PHP 5) iconv — 字符串按要求的字符编码来转换mb_convert_encoding — 转换字符的编码 这两个函数功能类似都是用来转

【转载】Perl中字符串编码的处理

在 Perl看来, 字符串只有两种形式. 一种是octets, 即8位序列, 也就是我们通常说的字节数组. 另一种utf8编码的字符串, perl管它叫string. 也就是说: Perl只熟悉两种编码: Ascii(octets)和utf8(string). utf8 flag在perl内部, 字符串结构由两部分组成: 数据和utf8 flag. 比如字符串"中国"在perl内部的存储是这样:utf8 flag 数据On 中国假如utf8 flag是On的话, perl就会把中国当成

判断字符串是不是数字

NumberUtils.isNumber(str)判断字符串是不是数字或者能不能转换成数字 public class StringIsNumber { public static void main(String[] args) { Scanner s = new Scanner(System.in); String str = s.nextLine(); if(NumberUtils.isNumber(str)){ System.out.println("输入的是数字"); }els

判断字符串是否为UTF8编码

UTF-8(8-bit Unicode Transformation Format)是一种针对Unicode的可变长度字符编码.由Ken Thompson于1992年创建.现在已经标准化为RFC 3629.UTF-8用1到4个字节编码Unicode字符.用在网页上可以统一页面显示中文简体繁体及其它语言(如英文,日文,韩文). <?php /** *检查字符串是否是utf8编码 *@param string $string 被检测字符串 *@return Boolean */ function i

判断字符串是否为 utf-8 编码

代码清单: <?php /** * 判断字符串是否为utf8编码,英文和半角字符返回ture * @author ruxing.li * @param $string * @return bool */ function is_utf8($string) { return preg_match('%^(?: [\x09\x0A\x0D\x20-\x7E] # ASCII | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte | \xE0[\xA0-\xBF

python 字符串编码 str和unicode 区别以及相互转化 decode(&#39;utf-8&#39;) encode(&#39;utf-8&#39;)

python 字符串编码 str和unicode 区别以及相互转化 decode('utf-8') encode('utf-8') 原文地址:https://www.cnblogs.com/zhaoyingjie/p/9133020.html