Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
853 views
in Technique[技术] by (71.8m points)

wordpress - PHP: strlen returns character length instead of byte length

I have a wordpress web site.

I've created simple page template like:

<?php 
 /**
 * Template Name: Test
 */

 echo strlen('Привет');

 ?>

Then i've created a page using this template. The page shows the length of russian string 'Привет' (means 'Hello'). I expect to see 12, as UTF-8 encoded russian string consisting of 6 characters should have a size of 12 bytes, but i get 6 instead.

I've tested the same thing on other server and had correct value - 12. So i think the reason is my server configuration. I have wp 3.2.1 (i had the same problem after upgrading to wp 3.5.1) and PHP 5.3.3.

Currently i've spent about 5 days trying to find a solution, but have no luck. Does anyone know what is the reason of such behavior?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Check the mbstring.func_overload setting in php.ini. This option allows PHP to override the strlen() function with mb_strlen() (and similarly for other equivalents). This could explain the discrepancy between your servers

EDIT

Quoting from the doc link:

To use function overloading, set mbstring.func_overload in php.ini to a positive value that represents a combination of bitmasks specifying the categories of functions to be overloaded. It should be set to 1 to overload the mail() function. 2 for string functions, 4 for regular expression functions. For example, if it is set to 7, mail, strings and regular expression functions will be overloaded.

So a value with the 2 bit set means that basic string functions will be overloaded with their mbstring equivalent, but not mail or regular expression functions; if you want normal behaviour, this should be 0


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...