每55个字符后插入一个Break

时间:2016-01-26 12:28:04

标签: php jquery

我想在每个 55 字符之后插入换行符我已经制作了一个jquery脚本,但是它不起作用可以请一些人指点我朝着正确的方向

的jquery.js

$(document).ready(function() {
    $('#text_comment').text($(this).val().replace(/([\s\S]{55})/g, '$1<br />'));

});

的index.php

<input type="text" class="comment" id="text_comment"autocomplete="off"time_date="<?php echo $date_shared;?>" 
    username="<?php echo $comment_username;?>"post_id="<?php echo $shared_id2; ?>"placeholder="Write a Comment"/>

2 个答案:

答案 0 :(得分:0)

inputs的换行符为automatically removed,类型为text

  

text:单行文本字段;换行符会自动从输入值中删除。

您可以使用textarea来显示换行符:

var myString = '1234567890';
myString = myString.replace(/.{1,2}/g, "$&\n"); //replace 2 with 55 for your example

document.getElementById('text').value = myString; // :(
document.getElementById('textarea').value = myString; // :)
<input type="text" id="text"> <-- line-breaks automatically removed
<br>
<textarea id="textarea" rows="7"></textarea>

答案 1 :(得分:0)

您可以使用此方法。

$original = "Sample String.";
$parts = str_split($original, 4); //Replace '4' with '55' afterwards.
$final = implode("<br />", $parts);