目前共有9篇帖子。
MySQL字段长度是这样统计的:所有字符都被认为是1个字符
1樓 巨大八爪鱼 2015-2-14 22:25
比如“正体中文abc”被认为是7个字符,“我I你you”被认为是六个字符,就连标点符号也被认为是一个字符。所有的汉字都算一个字符而非两个。
因此把“正体中文abc”插入一个varchar(7)的字段是可以的,但插入“正体中文abc,”就不行了,虽然记录能成功插入但会把末尾的逗号吞掉
2樓 巨大八爪鱼 2015-2-14 22:25
<article class="content">
    <h1>For PHP</h1>
    <section>
     <h2>Using strlen</h2>
     <p>
     <?php
     $strs = array();
     $strs[0] = "simple";
     $strs[1] = "¿español?";
     $strs[2] = "estómago; ¡díficil!";
     $strs[3] = "这是一段测试内容。";
     $strs[4] = "这是另1段测试内容";
     $strs[5] = "台湾正,体中文";
     $strs[6] = "文";
     $strs[7] = ",";
     $strs[8] = "ñ";
     $strs[9] = "¿";
     $strs[10] = "?";
     
     foreach ($strs as $str) {
         $n = strlen($str);
         if ($n == 1) {
            echo "String '$str' has <b>1</b> character.<br>";
         } else {
            echo "String '$str' has <b>$n</b> characters.<br>";
         }
     }
     ?></p>
    </section>
    <!-- end .content --></article>

For PHP Using strlen String 'simple' has 6 characters.
String '¿español?' has 11 characters.
String 'estómago; ¡díficil!' has 22 characters.
String '这是一段测试内容。' has 27 characters.
String '这是另1段测试内容' has 25 characters.
String '台湾正,体中文' has 19 characters.
String '文' has 3 characters.
String ',' has 1 character.
String 'ñ' has 2 characters.
String '¿' has 2 characters.
String '?' has 1 character.


可见,对于PHP的strlen函数,把汉字当成了3个字符,西班牙语特殊字符(倒问号,倒感叹号,字母ñ,带重音符号的字母)当成了2个字符,而英文字母和标点当成1个字符!
因此,不能用strlen函数来判断字符串长度是否符合mysql的要求。

如果用户输入的内容包含html特殊字符,就需要用htmlspecialchars转换,那么字符就会多出几个。不过mysql_real_escape_string函数不用管,mysql会自动将特殊字符转换回来。
3樓 巨大八爪鱼 2015-2-14 22:26
如果改用mb_strlen函数,问题就得到了解决。

echo "<br><br>";
     foreach ($strs as $str) {
         $n = mb_strlen($str);
         if ($n == 1) {
            echo "String '$str' has <i>actually</i> <b>1</b> character.<br>";
         } else {
            echo "String '$str' has <i>actually</i> <b>$n</b> characters.<br>";
         }
     }

String 'simple' has actually 6 characters.
String '¿español?' has actually 9 characters.
String 'estómago; ¡díficil!' has actually 19 characters.
String '这是一段测试内容。' has actually 9 characters.
String '这是另1段测试内容' has actually 9 characters.
String '台湾正,体中文' has actually 7 characters.
String '文' has actually 1 character.
String ',' has actually 1 character.
String 'ñ' has actually 1 character.
String '¿' has actually 1 character.
String '?' has actually 1 character.

因此可以在用户输入数据后,先进行htmlspecialchars编码(如果需要的话),然后用mb_strlen函数对字符串长度进行比较,如果超出数据库中设定的字段长度就提示错误就行了。
4樓 巨大八爪鱼 2015-2-14 22:26
<h1>For JavaScript</h1>
    <section>
     <p>
     <script>
     var strs = [];
     strs[0] = "simple";
     strs[1] = "¿español?";
     strs[2] = "estómago; ¡díficil!";
     strs[3] = "这是一段测试内容。";
     strs[4] = "这是另1段测试内容";
     strs[5] = "台湾正,体中文";
     strs[6] = "文";
     strs[7] = ",";
     strs[8] = "ñ";
     strs[9] = "¿";
     strs[10] = "?";
     
     for (var i = 0; i < strs.length; i++) {
         var n = strs[i].length;
         if (n == 1) {
            document.write("String '" + strs[i] + "' has <b>1</b> character.<br>");
         } else {
            document.write("String '" + strs[i] + "' has <b>" + n + "</b> characters.<br>");
         }
     }
     </script>
     </p>
    </section>

For JavaScript String 'simple' has 6 characters.
String '¿español?' has 9 characters.
String 'estómago; ¡díficil!' has 19 characters.
String '这是一段测试内容。' has 9 characters.
String '这是另1段测试内容' has 9 characters.
String '台湾正,体中文' has 7 characters.
String '文' has 1 character.
String ',' has 1 character.
String 'ñ' has 1 character.
String '¿' has 1 character.
String '?' has 1 character.


因此对于javascript,直接使用str.length就行了,得到的就是正确的结果。
5樓 巨大八爪鱼 2015-2-14 22:26
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style type="text/css">
<!--
body {
    font: 100%/1.4 Verdana, Arial, Helvetica, sans-serif;
    background-color: #42413C;
    margin: 0;
    padding: 0;
    color: #000;
}
/* ~~ Element/tag selectors ~~ */
ul, ol, dl { /* Due to variations between browsers, it's best practices to zero padding and margin on lists. For consistency, you can either specify the amounts you want here, or on the list items (LI, DT, DD) they contain. Remember that what you do here will cascade to the .nav list unless you write a more specific selector. */
    padding: 0;
    margin: 0;
}
h1, h2, h3, h4, h5, h6, p {
    margin-top: 0;     /* removing the top margin gets around an issue where margins can escape from their containing block. The remaining bottom margin will hold it away from any elements that follow. */
    padding-right: 15px;
    padding-left: 15px; /* adding the padding to the sides of the elements within the blocks, instead of the block elements themselves, gets rid of any box model math. A nested block with side padding can also be used as an alternate method. */
}
a img { /* this selector removes the default blue border displayed in some browsers around an image when it is surrounded by a link */
    border: none;
}
/* ~~ Styling for your site's links must remain in this order - including the group of selectors that create the hover effect. ~~ */
a:link {
    color: #42413C;
    text-decoration: underline; /* unless you style your links to look extremely unique, it's best to provide underlines for quick visual identification */
}
a:visited {
    color: #6E6C64;
    text-decoration: underline;
}
a:hover, a:active, a:focus { /* this group of selectors will give a keyboard navigator the same hover experience as the person using a mouse. */
    text-decoration: none;
}
/* ~~ This fixed width container surrounds all other blocks ~~ */
.container {
    width: 960px;
    background-color: #FFFFFF;
    margin: 0 auto; /* the auto value on the sides, coupled with the width, centers the layout */
}
/* ~~ The header is not given a width. It will extend the full width of your layout. ~~ */
header {
    background-color: #ADB96E;
}
/* ~~ These are the columns for the layout. ~~

1) Padding is only placed on the top and/or bottom of the block elements. The elements within these blocks have padding on their sides. This saves you from any "box model math". Keep in mind, if you add any side padding or border to the block itself, it will be added to the width you define to create the *total* width. You may also choose to remove the padding on the element in the block element and place a second block element within it with no width and the padding necessary for your design.

2) No margin has been given to the columns since they are all floated. If you must add margin, avoid placing it on the side you're floating toward (for example: a right margin on a block set to float right). Many times, padding can be used instead. For blocks where this rule must be broken, you should add a "display:inline" declaration to the block element's rule to tame a bug where some versions of Internet Explorer double the margin.

3) Since classes can be used multiple times in a document (and an element can also have multiple classes applied), the columns have been assigned class names instead of IDs. For example, two sidebar blocks could be stacked if necessary. These can very easily be changed to IDs if that's your preference, as long as you'll only be using them once per document.

4) If you prefer your nav on the left instead of the right, simply float these columns the opposite direction (all left instead of all right) and they'll render in reverse order. There's no need to move the blocks around in the HTML source.

*/
.sidebar1 {
    float: right;
    width: 180px;
    background-color: #EADCAE;
    padding-bottom: 10px;
}
.content {
    padding: 10px 0;
    width: 780px;
    float: right;
}

/* ~~ This grouped selector gives the lists in the .content area space ~~ */
.content ul, .content ol {
    padding: 0 15px 15px 40px; /* this padding mirrors the right padding in the headings and paragraph rule above. Padding was placed on the bottom for space between other elements on the lists and on the left to create the indention. These may be adjusted as you wish. */
}

/* ~~ The navigation list styles (can be removed if you choose to use a premade flyout menu like Spry) ~~ */
nav ul{
    list-style: none; /* this removes the list marker */
    border-top: 1px solid #666; /* this creates the top border for the links - all others are placed using a bottom border on the LI */
    margin-bottom: 15px; /* this creates the space between the navigation on the content below */
}
nav li {
    border-bottom: 1px solid #666; /* this creates the button separation */
}
nav a, nav a:visited { /* grouping these selectors makes sure that your links retain their button look even after being visited */
    padding: 5px 5px 5px 15px;
    display: block; /* this gives the link block properties causing it to fill the whole LI containing it. This causes the entire area to react to a mouse click. */
    width: 160px;  /*this width makes the entire button clickable for IE6. If you don't need to support IE6, it can be removed. Calculate the proper width by subtracting the padding on this link from the width of your sidebar container. */
    text-decoration: none;
    background-color: #C6D580;
}
nav a:hover, nav a:active, nav a:focus { /* this changes the background and text color for both mouse and keyboard navigators */
    background-color: #ADB96E;
    color: #FFF;
}

/* ~~ The footer ~~ */
footer {
    padding: 10px 0;
    background-color: #CCC49F;
    position: relative;/* this gives IE6 hasLayout to properly clear */
    clear: both; /* this clear property forces the .container to understand where the columns end and contain them */
}

/*HTML 5 support - Sets new HTML 5 tags to display:block so browsers know how to render the tags properly. */
header, section, footer, aside, article, figure {
    display: block;
}
-->
</style><!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]--></head>

<body>

<div class="container">
  <header>
    <a href="#"><img src="" alt="Insert Logo Here" width="180" height="90" id="Insert_logo" style="background-color: #C6D580; display:block;" /></a>
  </header>
  <div class="sidebar1">
  <nav>
    <ul>
      <li><a href="#">Link one</a></li>
      <li><a href="#">Link two</a></li>
      <li><a href="#">Link three</a></li>
      <li><a href="#">Link four</a></li>
    </ul>
    </nav>
    <aside>
      <p> The above links demonstrate a basic navigational structure using an unordered list styled with CSS. Use this as a starting point and modify the properties to produce your own unique look. If you require flyout menus, create your own using a Spry menu, a menu widget from Adobe's Exchange or a variety of other javascript or CSS solutions.</p>
      <p>If you would like the navigation along the top, simply move the ul to the top of the page and recreate the styling.</p>
    </aside>
  <!-- end .sidebar1 --></div>
  <article class="content">
    <h1>For PHP</h1>
    <section>
     <h2>Using strlen</h2>
     <p>
     <?php
     $strs = array();
     $strs[0] = "simple";
     $strs[1] = "¿español?";
     $strs[2] = "estómago; ¡díficil!";
     $strs[3] = "这是一段测试内容。";
     $strs[4] = "这是另1段测试内容";
     $strs[5] = "台湾正,体中文";
     $strs[6] = "文";
     $strs[7] = ",";
     $strs[8] = "ñ";
     $strs[9] = "¿";
     $strs[10] = "?";
     
     foreach ($strs as $str) {
         $n = strlen($str);
         if ($n == 1) {
            echo "String '$str' has <b>1</b> character.<br>";
         } else {
            echo "String '$str' has <b>$n</b> characters.<br>";
         }
     }
     
     ?></p>
     <h2>Using mb_strlen</h2>
     <p>
     <?php
     foreach ($strs as $str) {
         $n = mb_strlen($str);
         if ($n == 1) {
            echo "String '$str' has <i>actually</i> <b>1</b> character.<br>";
         } else {
            echo "String '$str' has <i>actually</i> <b>$n</b> characters.<br>";
         }
     }
     
     
     ?></p>
    </section>
    <h1>For JavaScript</h1>
    <section>
     <p>
     <script>
     var strs = [];
     strs[0] = "simple";
     strs[1] = "¿español?";
     strs[2] = "estómago; ¡díficil!";
     strs[3] = "这是一段测试内容。";
     strs[4] = "这是另1段测试内容";
     strs[5] = "台湾正,体中文";
     strs[6] = "文";
     strs[7] = ",";
     strs[8] = "ñ";
     strs[9] = "¿";
     strs[10] = "?";
     
     for (var i = 0; i < strs.length; i++) {
         var n = strs[i].length;
         if (n == 1) {
            document.write("String '" + strs[i] + "' has <b>1</b> character.<br>");
         } else {
            document.write("String '" + strs[i] + "' has <b>" + n + "</b> characters.<br>");
         }
     }
     </script>
     </p>
    </section>
    <!-- end .content --></article>
  <footer>
    <p>This footer contains the declaration position:relative; to give Internet Explorer 6 hasLayout for the footer and cause it to clear correctly. If you're not required to support IE6, you may remove it.</p>
    <address>
      Address Content
    </address>
  </footer>
  <!-- end .container --></div>
</body>
</html>
6樓 巨大八爪鱼 2015-2-14 22:26
总而言之,为了和MySQL数据库的字符串长度计算机制保持一致,在JavaScript中应该直接使用.length方法,而在PHP中则需要用mb_strlen函数。

顺便提一下,PHH中截取子字符串也可以用mb_substr函数,避免出现乱码。
7樓 巨大八爪鱼 2015-2-24 18:33
为了兼容各种服务器,使用mb_strlen之前,最好执行一下:
mb_internal_encoding("UTF-8");
设置mbstring库处理的默认字符集

我发现有的服务器上运行echo mb_strlen("汉字");输出的结果还是6
8樓 巨大八爪鱼 2015-2-25 17:19
还有一个更简单的方法:使用表单控件的maxlength属性,其单位与MySQL中的完全一样。
<label for="textfield">Text Field:</label>
<input name="textfield" type="text" id="textfield" maxlength="6">
<label for="textarea"><br>
Text Area:</label>
<textarea name="textarea" id="textarea" style="margin-top:5px" maxlength="10"></textarea>
在第二个大文本框中,可以输入“ab额as额ds啊啊”,刚好是10个字符。
9樓 巨大八爪鱼 2015-2-25 17:21
不过,最好还是在服务器端表单处理程序中检查一下字符串长度,以免用户输入HTML特殊符号,比如“<”而导致bug。比如在HTML中“<”会被编码为&lt;,这样一个<在数据库中就是存储的是4个字符而非1个。

回復帖子

內容:
用戶名: 您目前是匿名發表
驗證碼:
 
 
©2010-2024 Arslanbar [手機版] [桌面版]
除非另有聲明,本站採用創用CC姓名標示-相同方式分享 3.0 Unported許可協議進行許可。