简体   繁体   English

php nl2br限制x数量

[英]php nl2br limit x amount

Hi this is fairly simple 嗨,这很简单

I want to know how to use nl2br(); 我想知道如何使用nl2br(); in php, but limit the amount of consecutive <br/>'s that are allowed at one time. 在php中,但限制一次允许的连续<br/>数量。

//For Example: A user enters //例如:用户输入

1st line

2 line breaks



3 line breaks





6 line breaks


2 line breaks




Is there anyway to have php limit the <br/>'s to no more than x amount of numbers at at time 无论如何,有一次php将<br/>限制为一次不超过x个数字

so if we only allowed 4 <br>'s at a time the output would be 因此,如果一次只允许4个<br>,则输出为

1st line

2 line breaks



3 line breaks




6 line breaks (changed to 4)


2 line breaks

Try a regular expression to limit sequential newlines before passing things into nl2br: 在将事物传递到nl2br之前,尝试使用正则表达式来限制顺序换行符:

$clean = preg_replace('/\n{4,}/', '\n', preg_replace('/\r/', '', $dirty));

For the uninitiated: 对于初学者:

  • \\r is a Carriage Return, which we can safely strip before calling nl2br \\ r是回车符,我们可以在致电nl2br之前安全地剥离它
  • \\n is a Newline \\ n是换行符
  • {4,} means "repeat the last character four or more times {4,}表示“将最后一个字符重复四次或更多次

So, any sequence of four or more newlines in a row will be reduced to one. 因此,连续的四个或更多换行符的任何序列都将减少为一个。

Unfortunately this is easily defeated. 不幸的是,这很容易被击败。 A user can simply come by and hit enter + space + enter + space + enter + space... 用户只需走过去然后按Enter +空格+ Enter +空格+ Enter +空格...

It's a start, though. 不过,这是一个开始。 Let's make it a bit more idiot-proof. 让我们使它更防白痴。

$dirty = preg_replace('/\r/', '', $dirty);
$clean = preg_replace('/\n{4,}/', '\n', preg_replace('/^\s+$/m', '', $dirty));

Now we're also: 现在我们还:

  • Setting the "m" modifier so... 将“ m”修饰符设置为...
  • The ^ means "the beginning of a line" (without "m" it means the beginning of the entire string) ^表示“一行的开头”(不带“ m”表示整个字符串的开头)
  • The $ means "the end of a line" (without "m" it means the end of the entire string) $表示“一行的结尾”(不带“ m”表示整个字符串的结尾)
  • \\s+ means "any space character, repeating" \\ s +表示“任何空格字符,重复”

So, now it removes \\r, then changes lines that are nothing but spaces to being empty, then it limits empty lines. 因此,现在它删除\\ r,然后将除了空格以外的所有行更改为空,然后限制空行。

Beware, this may break formatting, depending on what markup you're using. 当心,这可能会破坏格式,具体取决于您使用的标记。 Certain markup engines can operate on lines that are pure-whitespace and do smart things with them, such as the markup engine used here at SO. 某些标记引擎可以在纯空白的行上运行并对其执行智能操作,例如SO此处使用的标记引擎。

Have you considered banning jerks? 您是否考虑过禁止抽搐? It's sometimes effective. 有时很有效。 :) :)

Got it guys, this wont allow anymore than x amount of line breaks at a time. 知道了,这一次最多只能允许x个换行。

$string = the input were modifying $ string =输入正在修改
$num = the amount of line breaks we wont allow the user to go over $ num =我们不允许用户翻阅的换行数量

function nl2br_limit($string, $num){

    return preg_replace('/\n/', '<br/>', preg_replace('/(\s{'.$num.'})\s+/','$1', $string));
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM