English 中文(简体)
用单一新线取代多个新线
原标题:Replace multiple new lines with a single newline

我如何用一条新路线取代连续的多条新线。 可能相互之间有20条新路线。 例如

James said hello



 Test
 Test two


最终如下:

James said hello
 Test
 Test two

最佳回答

引证:

$str = "Hello




World


How
Are

You?";
$str = preg_replace("/
+/", "
", $str);
print($str);
问题回答

改进马克 B 答案:

$fixed_text  = preg_replace("
(s*
)+", "
", $text_to_fix);

这条新线应当与最初的新线相匹配,然后至少是一组白空间,然后是一条新线,用一条新线取代。

$fixed_text = preg_replace("
+", "
", $text_to_fix);

This should do it, assuming that the consecutive newlines are truly consecutive and don t have any whitespace (tabs, spaces, carriage returns, etc...) between them.

$str =  James said hello



 Test
 Test two

 ;
echo preg_replace( {(\
)1+} , $1 ,$str);

In regex:

  • + means "one or more of the previous expression"
  • {2,} means "two or more of the previous expression"

For the requirements in this question, there is no point replacing a single with a single because nothing changes for that substring. In other words, / +/ is simply doing more work than necessary.

采用两个或两个以上层次的验证方法更有意义。

法典:Demo

$string = "James said hello



 Test
 Test two

";
echo json_encode(
    preg_replace("/
{2,}/", "
", $string)
);

产出:

"James said hello
 Test
 Test two
"

In other situations, it may be advantageous to replace newline sequences that may be from different operating systems with R. This metacharacter matches or . Here is a modified pattern which will replace two or more newline sequences with the server s designated newline character sequence:

$string = "James
 said

 hello


 Test
 Test two

";
echo json_encode(
    preg_replace("/R{2,}/", PHP_EOL, $string)
);
// "James
 said
 hello
 Test
 Test two
"
//       ^^^^----treated as one newline character sequence




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签