【发布时间】:2013-09-10 12:35:38
【问题描述】:
这是lib.php中的代码:
<?php
class table {
function __construct($string, $time) {
$out = '<table cellpadding="5">';
$out .= $this->getRow('th', 'Nom., Shuffled String, Lenght');
for ($x = 1; $x <= $time; $x++) {
$shuffledStr = str_shuffle($string); //Maybe this causes the problem
$shuffledStr_len = strlen($shuffledStr);
$out .= $this->getRow('td', $x . ', ' . $shuffledStr . ', ' . $shuffledStr_len);
}
$out .= '</table>';
echo $out;
}
public function getRow($tagName, $contents_list) {
//Variables:
$out = '';
$contents_array = explode(', ', $contents_list);
$contents_number = count($contents_array);
$start_tag = '<' . $tagName . '>';
$end_tag = '</' . $tagName . '>';
// Build
$out .= '<tr>';
for ($i = 0; $i < $contents_number; $i++) {
$out .= $start_tag . $contents_array[$i] . $end_tag;
}
$out .= '</tr>';
return $out;
}
}
?>
这里是 index.php:
<?php
require_once 'lib.php';
$string = ''; //My string
$shuffleTimes = 100;
$table = new table($string, $shuffleTimes);
?>
这个程序得到一个字符串和你想洗牌的数字, 然后创建一个表并返回每行中的数字、洗牌字符串和洗牌字符串的长度。
例如,如果我将变量 $string 设置为“堆栈溢出”,它会正常工作(它随机打乱这个词 100 次,返回所有长度 14 并且 ALL 的长度被打乱字符串实际上是 14。)
但是……
如果我向变量$string(例如Stack Overflow+_)(*&^%$#{}[]<>@!~./=-)添加一些特殊字符,它将无法正常工作。这意味着它返回长度 37 但它们没有 37 个字符!(例如,它打印 nothing 并打印它的长度 38。
我觉得这有点奇怪。 :(
这是为什么?!是哪个角色造成的,如何解决?
【问题讨论】:
-
Stack Overflow+_)(*&^%$#{}[]<>@!~./=-是 37 个字符长度...? -
是的。不是吗?
-
你说
But they doesn't have 37 characters!。现在怎么办?有没有37? p.s.你的代码对我有用。它只对多字节字符不起作用。 -
它返回
NOTHING并且还返回它有 37 个字符。什么是“多字节”字符?能解释一下或者给个链接吗?
标签: php string special-characters shuffle