- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我编写了一个脚本,将大段文本发送给 Google 进行翻译,但有时文本(即 html 源代码)最终会在 html 标记中间拆分,Google 将错误地返回代码。
我已经知道如何将字符串拆分成一个数组,但是有没有更好的方法来做到这一点,同时确保输出字符串不超过 5000 个字符并且不在标签上拆分?
更新:感谢回答,这是我最终在项目中使用的代码,效果很好
function handleTextHtmlSplit($text, $maxSize) {
//our collection array
$niceHtml[] = '';
// Splits on tags, but also includes each tag as an item in the result
$pieces = preg_split('/(<[^>]*>)/', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
//the current position of the index
$currentPiece = 0;
//start assembling a group until it gets to max size
foreach ($pieces as $piece) {
//make sure string length of this piece will not exceed max size when inserted
if (strlen($niceHtml[$currentPiece] . $piece) > $maxSize) {
//advance current piece
//will put overflow into next group
$currentPiece += 1;
//create empty string as value for next piece in the index
$niceHtml[$currentPiece] = '';
}
//insert piece into our master array
$niceHtml[$currentPiece] .= $piece;
}
//return array of nicely handled html
return $niceHtml;
}
最佳答案
注意:还没有机会对此进行测试(因此可能存在一两个小错误),但它应该给您一个想法:
function get_groups_of_5000_or_less($input_string) {
// Splits on tags, but also includes each tag as an item in the result
$pieces = preg_split('/(<[^>]*>)/', $input_string,
-1, PREG_SPLIT_DELIM_CAPTURE);
$groups[] = '';
$current_group = 0;
while ($cur_piece = array_shift($pieces)) {
$piecelen = strlen($cur_piece);
if(strlen($groups[$current_group]) + $piecelen > 5000) {
// Adding the next piece whole would go over the limit,
// figure out what to do.
if($cur_piece[0] == '<') {
// Tag goes over the limit, just put it into a new group
$groups[++$current_group] = $cur_piece;
} else {
// Non-tag goes over the limit, split it and put the
// remainder back on the list of un-grabbed pieces
$grab_amount = 5000 - $strlen($groups[$current_group];
$groups[$current_group] .= substr($cur_piece, 0, $grab_amount);
$groups[++$current_group] = '';
array_unshift($pieces, substr($cur_piece, $grab_amount));
}
} else {
// Adding this piece doesn't go over the limit, so just add it
$groups[$current_group] .= $cur_piece;
}
}
return $groups;
}
另请注意,这可以在常规单词的中间拆分 - 如果您不想那样,请修改以 //Non-tag goes over the limit
开头的部分以选择一个$grab_amount
的值(value)更高。我没有费心编写代码,因为这只是一个如何绕过拆分标签的示例,而不是一个直接解决方案。
关于php - 将大字符串拆分成数组,但拆分点不能打断标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3294430/
我正在使用原味 Markdown ,如here所述. 我想知道是否可以在 Markdown 代码中打断长行,同时不会产生语法效果。 (在其他语言中,例如 shell 脚本和 C,\ 将用于继续下一行。
我在显示从 0-10K 运行的图时遇到了问题。目前我有从 0-100 运行的计算,它看起来很棒。 目前: 现在我想加一个 X 点,也就是 10K它看起来是这样的: 如何将它保持在 0-100 之间,然
我在更广泛的范围内问这个问题,因为我现在没有面临这个具体问题,但我想知道将来该怎么做。 如果我有一个长时间运行的 python 脚本,它应该一直在做某事(如果有帮助的话,可以是一个无限循环)。通过在终
主题:利用python画图实现坐标轴截断或打断 关键词:python, plot, matplotlib, break axes 方法一: 首先介绍一种简单快速的方法――调用包 brokena
如果运行此代码并单击 P,整个捕获阶段将执行,但冒泡阶段将按预期在 div 上停止。这段代码有什么问题? for(let elem of document.querySelectorAll('*'
那是通常的“让 img 旁边的东西垂直居中”,但我需要两行。一行就好了,但看起来像 破坏它。 Centered text 结果: IMG IMG text here IMG 但是如果我想这样做:
我在 GNU/Linux 下用 C 语言编程的多线程服务器中有这种奇怪的行为。当它正在发送数据时,最终会被 SIGPIPE 中断。因此,我设法忽略了 send() 中的信号并在每次操作后处理 errn
我有一个 ListView,它有一个方法 (public ArrayAdapter populateListView(){}) 来更新适配器。我每 500 毫秒调用一次 lv.setAdapter(p
我想将 div 堆叠在一起并在三个子元素之后打断 div。 示例 HTML: hello hello hello hello hello hello
我是一名优秀的程序员,十分优秀!