php substr()函数截取中文字符串乱码
在php中如果我要用substr()截取字符串全英文的没问题,如果包括有中文或英文就会悲剧了,但大家也 别切我们可以使用其它办法来解决.
php截取中文字符串出现乱码,这是最近发现的事情,先前我曾经写过一篇关于自动生成meta信息的文章,那篇关于利用php截取文章前多少字作为description方法,但是出现了IE6无法加载CSS的现象,这里做一个补充。
首先要明确这么一个问题,之所以会出现IE6偶尔无法加载CSS的现象,是因为文件出现了乱码,导致后面的加载CSS的link无法被IE6正确解析,因此就看到了一个纯HTML页面,没有CSS,赤裸裸!
明确了问题,剩下的问题就好解决了,就是防止乱码,既然万戈所提供的函数出现了乱码,就重新去找了一个php函数来解决这个乱码的问题。
substr()函数可以分割文字,但要分割的文字如果包括中文字符往往会遇到问题。
mb_substr()这个函数的用法与substr()相似,只是在最后要加入多一个参数,以设定字符串的编码,通过这里大几就应该理解我改进万戈方法的原因了~~下面再介绍几个更高级处理办法
例1代码如下:
<?php function func_chgtitle($str, $len) { //$length我们允许字符串显示的最大长度 $tmpstr = ""; $strlen = $len; for ($i = 0; $i < $strlen; $i++) { if (ord(substr($str, $i, 1)) > 0xa0) { $tmpstr.= substr($str, $i, 2); $i++; } else $tmpstr.= substr($str, $i, 1); } return $tmpstr; } ?>
例2字符串编码为UTF-8的,一个中文字符占三个字节:
<?php public static function chinesesubstr($str, $start, $len) { // $str指字符串,$start指字符串的起始位置,$len指字符串长度 $strlen = $start + $len; // 用$strlen存储字符串的总长度,即从字符串的起始位置到字符串的总长度 for ($i = $start; $i < $strlen;) { if (ord(substr($str, $i, 1)) > 0xa0) { // 如果字符串中首个字节的ASCII序数值大于0xa0,则表示汉字 $tmpstr.= substr($str, $i, 3); // 每次取出三位字符赋给变量$tmpstr,即等于一个汉字 $i = $i + 3; // 变量自加3 } else { $tmpstr.= substr($str, $i, 1); // 如果不是汉字,则每次取出一位字符赋给变量$tmpstr $i++; } } return $tmpstr; // 返回字符串 } ?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Go language provides two dynamic function creation technologies: closure and reflection. closures allow access to variables within the closure scope, and reflection can create new functions using the FuncOf function. These technologies are useful in customizing HTTP routers, implementing highly customizable systems, and building pluggable components.

In C++ function naming, it is crucial to consider parameter order to improve readability, reduce errors, and facilitate refactoring. Common parameter order conventions include: action-object, object-action, semantic meaning, and standard library compliance. The optimal order depends on the purpose of the function, parameter types, potential confusion, and language conventions.

1. The SUM function is used to sum the numbers in a column or a group of cells, for example: =SUM(A1:J10). 2. The AVERAGE function is used to calculate the average of the numbers in a column or a group of cells, for example: =AVERAGE(A1:A10). 3. COUNT function, used to count the number of numbers or text in a column or a group of cells, for example: =COUNT(A1:A10) 4. IF function, used to make logical judgments based on specified conditions and return the corresponding result.

The key to writing efficient and maintainable Java functions is: keep it simple. Use meaningful naming. Handle special situations. Use appropriate visibility.

The advantages of default parameters in C++ functions include simplifying calls, enhancing readability, and avoiding errors. The disadvantages are limited flexibility and naming restrictions. Advantages of variadic parameters include unlimited flexibility and dynamic binding. Disadvantages include greater complexity, implicit type conversions, and difficulty in debugging.

The benefits of functions returning reference types in C++ include: Performance improvements: Passing by reference avoids object copying, thus saving memory and time. Direct modification: The caller can directly modify the returned reference object without reassigning it. Code simplicity: Passing by reference simplifies the code and requires no additional assignment operations.

The difference between custom PHP functions and predefined functions is: Scope: Custom functions are limited to the scope of their definition, while predefined functions are accessible throughout the script. How to define: Custom functions are defined using the function keyword, while predefined functions are defined by the PHP kernel. Parameter passing: Custom functions receive parameters, while predefined functions may not require parameters. Extensibility: Custom functions can be created as needed, while predefined functions are built-in and cannot be modified.

Exception handling in C++ can be enhanced through custom exception classes that provide specific error messages, contextual information, and perform custom actions based on the error type. Define an exception class inherited from std::exception to provide specific error information. Use the throw keyword to throw a custom exception. Use dynamic_cast in a try-catch block to convert the caught exception to a custom exception type. In the actual case, the open_file function throws a FileNotFoundException exception. Catching and handling the exception can provide a more specific error message.
