简体   繁体   English

“我正在尝试将文本换成一行但没有换行符

[英]"m trying to echo a text into one line with no line breaks

I"m trying to echo a text into one line with no line breaks. I've tried to use nl2br and than replace all /n or /r with nothing. I want the output to be: 我正在尝试将文本回显为没有换行符的一行。我尝试使用nl2br,而不是全部替换/ n或/ r。我希望输出为:

"I"m trying to write my text in one line" “我正在尝试将我的文字写成一行”

instead of: 代替:

"I"m trying to write my text 我正在写我的文字

in one line" 一排”

<!DOCTYPE html>
<html>
<body>

<?php

$mytext = 'I"m trying to write my text
in one line';  

$mytext = nl2br($mytext);
$mytext = preg_replace('/\r?\n|\r/','', $mytext);
$mytext = str_replace(array("\r\n","\r","\n"),"", $mytext);
echo $mytext;

?> 

</body>
</html>

Other than not using nl2br , and needing to replace with a space, not an empty string, your code should work fine (note you can optimise the preg_replace by using \\R which matches any unicode newline sequence): 除了不使用nl2br ,并且需要用空格而不是空字符串替换之外,您的代码应该可以正常工作(请注意,您可以使用\\R来优化preg_replace ,该\\R可以匹配任何unicode换行符序列):

$mytext = "I'm trying to write my text\r
in one line";  

echo preg_replace('/\R/',' ', $mytext);
echo PHP_EOL;
echo str_replace(array("\r\n", "\r","\n"), " ", $mytext);

Output: 输出:

I'm trying to write my text in one line
I'm trying to write my text in one line

Now if you want to make sure it remains on one line in the HTML, you need to replace spaces with non-breaking spaces eg 现在,如果要确保它保留在HTML中的一行上,则需要用不间断的空格替换空格,例如

$mytext = str_replace(' ', '&nbsp;', $mytext);

Here's a demo using &nbsp; 这是使用&nbsp;的演示 in JS: 在JS中:

 let str = "I'm trying to write my text in one line I'm trying to write my text in one line I'm trying to write my text in one line"; $('#d1').html(str); str = str.replace(/\\s/g, '&nbsp;'); $('#d2').html(str); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="d1" style="width: 200px;"></div> <div id="d2" style="width: 200px;"></div> 

Just preg_replace will do the trick. 只要preg_replace就能解决问题。 If the only character you need to replace is newlines, try the following: 如果您需要替换的唯一字符是换行符,请尝试以下操作:

$mytext = 'I"m trying to write my text
in one line';
echo preg_replace('/\n/', ' ', $mytext); // prints I"m trying to write my text in one line

Even though you don't explicitly include a newline character in your multi-line string, the line break will be interpreted as a newline character so you can identify it using \\n in your regex. 即使您没有在多行字符串中明确包含换行符,该换行符也将被解释为换行符,因此您可以在正则表达式中使用\\n进行识别。

The reason your current solution isn't working as expected is that nl2br inserts html line break elements into the string, not newline characters. 您当前的解决方案无法按预期运行的原因是nl2br html nl2br插入到字符串中,而不是换行符中。 See the docs here: https://www.php.net/manual/en/function.nl2br.php . 在此处查看文档: https : //www.php.net/manual/zh/function.nl2br.php When I run your solution it prints I"m trying to write my text<br />in one line - the newline characters are being removed, but not the html elements added by nl2br . 当我运行您的解决方案时,它会打印I"m trying to write my text<br />in one line -删除换行符,但不删除nl2br添加的html元素。

In the future, please include the incorrect output you're seeing in your post along with the code! 将来,请在代码中包含您看到的错误输出以及代码!

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

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