English 中文(简体)
Javascript String Length Differs from PHP mb_strlen
原标题:Javascript String Length Differs From PHP mb_strlen

I use document.getElementById(“text”). Value.length to have the string length through javascript, and mb_strlen($_POST[ text]) to have the string length by PHP and both differencess. 运回货物在拖长之前用java字母换算,但我猜测一些特性并不算。

例如,

[b]15。 Umieszczanie obrazka zlogo na stronie zespovalu [/b]

这份案文按javascript和PHP计算为57个。 当案文长时,差异会增加。 是否有办法解决这一问题?

问题回答

如果你再次试图在PHP中填满UTF-8的编码,请具体说明在第二个参数<代码>mb_strlen<<<>/code>中的编码。

mb_strlen($_POST[ text ],  UTF-8 )

另外,也不要忘记在POST-var上打stripslashes

我发现, Java稿的血压当量功能对其他人可能有用:

function mb_strlen(str) {
    var len = 0;
    for(var i = 0; i < str.length; i++) {
        len += str.charCodeAt(i) < 0 || str.charCodeAt(i) > 255 ? 2 : 1;
    }
    return len;
}

感谢所有努力提供帮助的人!

我注意到,那里具有非标准性质——我不敢确定购买力平价如何计算非标准,但可以算作二者。 如果你在没有这种特性的情况下进行试验,会发生什么情况?

这应当做到trick。

function mb_strlen (s) {
  return ~-encodeURI(s).split(/%..|./).length;
}

Just type more than one line in your text area and you ll see the diference going bigger and bigger... This came from the fact Javascript value.length don t count the end of line when all PHP length functions take them in account. Just do:

// In case you re using CKEditot
// id is the id of the text area
var value = eval( CKEDITOR.instances. +id+ .getData(); );

// String length without the CRLF   
var taille = value.length;

// get number of line
var nb_lines = (value.match(/
/g) || []).length;

// Now, this value is the same you ll get with strlen in PHP
taille = taille + nb_lines;




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签