gpt4 book ai didi

php - jQuery 相当于 php 的 wordwrap?

转载 作者:可可西里 更新时间:2023-11-01 00:10:34 26 4
gpt4 key购买 nike

我正在寻找一种方法来使用 jQuery 来完成我在 php 中使用 wordwrap() ( http://php.net/manual/en/function.wordwrap.php ) 可以做的事情

我会使用 php,但我需要在加载 dom 后动态发生包装--

我对 jQuery 不够熟悉,无法从头开始编写此类函数,并且我运行了一些谷歌和堆栈搜索,试图找到预制插件或已经完成的东西。

有什么想法吗?或者也许是一个起点?

谢谢!

编辑:为了澄清,我需要能够在换行符处添加 html(例如 </p><p>)--

作为 php:

wordwrap($text, 8, "</p><p>", true);

最佳答案

http://james.padolsey.com/javascript/wordwrap-for-javascript/

function wordwrap( str, width, brk, cut ) {
brk = brk || '\n';
width = width || 75;
cut = cut || false;

if (!str) { return str; }

var regex = '.{1,' +width+ '}(\\s|$)' + (cut ? '|.{' +width+ '}|.+$' : '|\\S+?(\\s|$)');

return str.match( RegExp(regex, 'g') ).join( brk );
}

关于php - jQuery 相当于 php 的 wordwrap?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8423994/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com