Hướng dẫn how to remove\ n to string using php? - làm cách nào để xóa\ n thành chuỗi bằng php?

Làm thế nào để xóa "\ n" trong chuỗi? Tôi đã sử dụng 3 phương thức dưới đây, các đường may chỉ hoạt động "\ s+", nhưng tôi chỉ muốn xóa \ n, không phải tất cả không gian, làm thế nào để thực hiện công việc? Cần giúp đỡ, câu hỏi này đã làm tôi bối rối trong một thời gian dài.
I used 3 method bellow ,seams that only the "\s+" works , but I only want to remove \n,not all space ,how to do the job?
need help, this question had puzzled me for a long time.

$str="

hello
world

"; //$str=str_replace("\n","",$str); $str=preg_replace("@\n@","",$str); //$str=preg_replace("@\s+@"," ",$str); echo $str;

Đã hỏi ngày 10 tháng 5 năm 2013 lúc 7:16May 10, 2013 at 7:16

Hướng dẫn how to remove n to string using php? - làm cách nào để xóa n thành chuỗi bằng php?

mingfish_004mingfish_004mingfish_004

1.3252 huy hiệu vàng18 Huy hiệu bạc26 Huy hiệu đồng2 gold badges18 silver badges26 bronze badges

1

Điều này cũng nên làm thủ thuật.

$str=str_replace("\r\n","",$str);

Đã trả lời ngày 10 tháng 5 năm 2013 lúc 7:25May 10, 2013 at 7:25

4

$string = trim(preg_replace('/\s\s+/', ' ', $string));

Mã trên sẽ thay thế nhiều không gian và dòng mới bằng một không gian.

Gunaseelan

2.4095 huy hiệu vàng35 huy hiệu bạc42 Huy hiệu đồng5 gold badges35 silver badges42 bronze badges

Đã trả lời ngày 8 tháng 8 năm 2014 lúc 13:38Aug 8, 2014 at 13:38

Hosam Elzaghhosam ElzaghHosam Elzagh

1.15215 huy hiệu bạc25 huy hiệu đồng15 silver badges25 bronze badges

2

\ N trong PHP là gì?

text

text2

  • item1
  • item2

Sử dụng các thẻ dòng mới: Các ký tự mới \ n hoặc \ r \ n có thể được sử dụng để tạo một dòng mới bên trong mã nguồn.

text

text2>

  • item1
  • item2

Một biến PHP chứa chuỗi sau:

str_replace('\n', '', $str);
str_replace('\r', '', $str);
str_replace('\r\n\', '', $str);

Tôi muốn xóa tất cả các ký tự dòng mới trong chuỗi này để chuỗi sẽ trông như thế này:

Cải thiện bài viết

Lưu bài viết

Có thể xóa ngắt dòng khỏi chuỗi bằng cách sử dụng hàm str_replace (). Hàm str_replace () là một hàm sẵn có trong PHP được sử dụng để thay thế tất cả các lần xuất hiện của chuỗi tìm kiếm hoặc mảng chuỗi tìm kiếm bằng chuỗi thay thế hoặc mảng các chuỗi thay thế trong chuỗi đã cho hoặc mảng đã cho.str_replace() function. The str_replace() function is an inbuilt function in PHP which is used to replace all the occurrences of the search string or array of search strings by replacement string or array of replacement strings in the given string or array respectively.
Syntax: 
 

str_replace ($ searchVal, $ thay thế, $ chủ đề, $ đếm) & nbsp; & nbsp;
 

Loại trả về: Hàm này trả về một chuỗi hoặc mảng mới dựa trên tham số $ chủ đề với các giá trị được thay thế. Ví dụ: Sau khi thay thế thẻ, chuỗi mới được lấy trong văn bản biến. & Nbsp; & nbsp; This function returns a new string or array based on the $subjectVal parameter with replaced values.
Example: After replacing the
tag, the new string is taken in the variable text. 
 

PHP

$str=str_replace("\r\n","",$str);
0
$str=str_replace("\r\n","",$str);
1
$str=str_replace("\r\n","",$str);
2

$str=str_replace("\r\n","",$str);
4
$str=str_replace("\r\n","",$str);
0
$str=str_replace("\r\n","",$str);
3

$str=str_replace("\r\n","",$str);
4
$str=str_replace("\r\n","",$str);
8
$str=str_replace("\r\n","",$str);
3

Is

$str=str_replace("\r\n","",$str);
4
$str=str_replace("\r\n","",$str);
0
$str=str_replace("\r\n","",$str);
3

text

text2

  • item1
  • item2
3

Output:

Geeks
For
Geeks
GeeksForGeeks

$str=str_replace("\r\n","",$str);
4
$str=str_replace("\r\n","",$str);
8
$str=str_replace("\r\n","",$str);
3
preg_replace() function: The preg_replace() function is an inbuilt function in PHP which is used to perform a regular expression for search and replace the content.
Syntax: 
 

preg_replace( $pattern, $replacement, $subject, $limit, $count )

Is This function returns an array if the subject parameter is an array, or a string otherwise.
Example: This example use preg_replace() function to remove line break. 
 

PHP

$str=str_replace("\r\n","",$str);
0
$str=str_replace("\r\n","",$str);
1
$str=str_replace("\r\n","",$str);
2

$str=str_replace("\r\n","",$str);
4
$str=str_replace("\r\n","",$str);
0
$str=str_replace("\r\n","",$str);
3

$str=str_replace("\r\n","",$str);
4
$str=str_replace("\r\n","",$str);
8
$str=str_replace("\r\n","",$str);
3

Is

$str=str_replace("\r\n","",$str);
4
$str=str_replace("\r\n","",$str);
0
$str=str_replace("\r\n","",$str);
3

text

text2

  • item1
  • item2
3

Output:

Geeks
For
Geeks
GeeksForGeeks


Làm thế nào để xóa \ n khỏi chuỗi trong PHP?

Có thể xóa ngắt dòng khỏi chuỗi bằng cách sử dụng hàm str_replace ().using str_replace() function.

\ N có hoạt động trong PHP không?

\ N có hoạt động trong PHP không?Bạn có thể sử dụng các ký tự Php Newline \ n hoặc \ r \ n để tạo một dòng mới bên trong mã nguồn.Tuy nhiên, nếu bạn cũng muốn hiển thị ngắt dòng trong trình duyệt, bạn có thể sử dụng hàm php nl2br () để chèn các dòng HTML ngắt trước tất cả các dòng mới trong một chuỗi.You can use the PHP newline characters \n or \r\n to create a new line inside the source code. However, if you want the line breaks to be visible in the browser too, you can use the PHP nl2br() function which inserts HTML line breaks before all newlines in a string.

\ N trong PHP là gì?

Sử dụng các thẻ dòng mới: Các ký tự mới \ n hoặc \ r \ n có thể được sử dụng để tạo một dòng mới bên trong mã nguồn.create a new line inside the source code.