我正在尝试将段落拆分为100个字符的数组。但我的算法不起作用......
在我的输出中第一个我得到100个字符。但第二个我得到200,下一个我得到300我认为......
$desc = $this->getDescription();
$desc = preg_replace('/[^a-zA-Z0-9\s]/', ' ', strip_tags(html_entity_decode($desc)));
$desc = preg_replace('/(\s\s+|\t|\n)/', ' ', $desc);
$count = strlen($desc);
$time = ($count/100);
$x = 0;
$y = 100;
for($i = 0; $i<ceil($time);$i++){
$array[$i] = substr($desc,$x,$y);
$array[$i] = str_replace(" ", "+", $array[$i]);
$link[$i] = 'http://translate.google.com/translate_tts?tl=en&q=' . $array[$i];
$x+= 100;
$y+= 100;
}
有人知道这个问题吗?
我得到的输出:
我试图调试它......
Characters: 100 | x: 0 | y: 100 |
|Characters: 200 | x: 100 | y: 200 |
|Characters: 300 | x: 200 | y: 300 |
|Characters: 400 | x: 300 | y: 400 |
|Characters: 422 | x: 400 | y: 500 |
|Characters: 322 | x: 500 | y: 600 |
|Characters: 222 | x: 600 | y: 700 |
|Characters: 122 | x: 700 | y: 800 |
|Characters: 22 | x: 800 | y: 900
答案 0 :(得分:1)
$ y是长度,你在循环的每一步都将它增加100。
我建议评论或删除该行:
$y+= 100;
答案 1 :(得分:0)
试试这个
$str= "If the optional split_length parameter is specified, the returned array will be broken down into chunks with each being split_length in length, otherwise each chunk will be one character in length. If the optional split_length parameter is specified, the returned array will be broken down into chunks with each being split_length in length, otherwise each chunk will be one character in length. If the optional split_length parameter is specified, the returned array will be broken down into chunks with each being split_length in length, otherwise each chunk will be one character in length. If the optional split_length parameter is specified, the returned array will be broken down into chunks with each being split_length in length, otherwise each chunk will be one character in length. If the optional split_length parameter is specified, the returned array will be broken down into chunks with each being split_length in length, otherwise each chunk will be one character in length. If the optional split_length parameter is specified, the returned array will be broken down into chunks with each being split_length in length, otherwise each chunk will be one character in length. If the optional split_length parameter is specified, the returned array will be broken down into chunks with each being split_length in length, otherwise each chunk will be one character in length. If the optional split_length parameter is specified, the returned array will be broken down into chunks with each being split_length in length, otherwise each chunk will be one character in length.";
$arr = str_split($str, 100);
print"<pre>";
print_r($arr);