[英]new line using string replace
I have problems with getting data from mysql database.Let's say that I have text like this in mysql table: 我在从mysql数据库获取数据时遇到问题。假设在mysql表中有这样的文本:
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Suspendisse lobortis sit amet est in dapibus. Cras in dui diam.
This new line makes the problem. 这条新线造成了问题。
I'm using jQuery to echo data to auto creatinig div's. 我正在使用jQuery将数据回显到自动creatinig div。
Here's the code: 这是代码:
for(i = <?php echo $row; ?>; i > 0; i--){
count = 1;
<?php
while($col = mysqli_fetch_assoc($result))
{
$cols[] = $col;
}
foreach($cols as $col){
?>
$("#text"+count+"").text("<?php echo $col['post'] ?>");
$("#username"+count+"").text("<?php echo $col['username'] ?>");
count++;
<?php } ?>
}
I've tried to remove new line or to replace it with <br>
using string replace like str_replace("\\n", '<br />', $col['post']);
我试图删除新行或使用
str_replace("\\n", '<br />', $col['post']);
类的字符串替换用<br>
替换它 but it didn't work. 但这没用。 I've also tried many other codes and nothing worked for me.
我还尝试了许多其他代码,但对我没有任何帮助。
Thanks for suggestions 感谢您的建议
You would be much better off seprating out your DB access logic and the client-side javascript stuff. 分离数据库访问逻辑和客户端javascript内容会更好。 You can also use
nl2br()
to convert newlines to <br>
. 您还可以使用
nl2br()
将换行符转换为<br>
。 So something like this should work better. 所以这样的事情应该会更好。
<?php
$rows = array();
while($row = mysqli_fetch_assoc($result)) {
// replace new lines in post with HTML breaks
$row['post'] = nl2br($row['post']);
$rows[] = $row;
}
?>
<script>
// create javascript literal by echoing out JSON serialization of $rows from PHP
var dbRows = <?php echo json_encode($rows); ?>;
// iterate through DB records and create elements
$.each(dbRows, function(index, row) {
$('<div class="text">').text(row.post).appendTo('#someTargetElement');
$('<div class="username">').text(row.username).appendTo('#someTargetElement');
});
</script>
A few changes from your approach include: 您的方法有一些变化,包括:
nl2br()
on retrieved data from DB to prepare for use in HTML. nl2br()
以准备在HTML中使用。 #text*
anti-pattern. #text*
反模式。 What value do the id labels add? The first 首先
If the string output to a textarea or pre tag, \\n
will make new line in string. 如果字符串输出到textarea或pre标签,则
\\n
将在字符串中换行。 If you don't want to have new line, just remove \\n
. 如果您不想换行,只需删除
\\n
。
The second 第二
If the string output to another HTML tags, <br>
will make new line, just remove it to stop go to new line. 如果字符串输出到另一个HTML标记,
<br>
会换行,只需将其删除即可停止换行。
The third 第三
In PHP, you can have new line in long string by concat strings with const PHP_EOL
. 在PHP中,可以使用const
PHP_EOL
concat字符串在长字符串中PHP_EOL
。 Ex: "Hello" . PHP_EOL . "Wolrd"
例如:
"Hello" . PHP_EOL . "Wolrd"
"Hello" . PHP_EOL . "Wolrd"
In jQuery, $.text() will put escaped string to elment's text node (the content), $.html() will insert raw html (as string) to document. 在jQuery中,$ .text()会将转义的字符串放入elment的文本节点(内容),$。html()将原始html(作为字符串)插入文档。
The last 最后
Please don't put your PHP codes together Javascript. 请不要将您的PHP代码放到Javascript中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.