Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将mySQL中存储的换行符转换为一行_Php_Tags_Meta - Fatal编程技术网

Php 如何将mySQL中存储的换行符转换为一行

Php 如何将mySQL中存储的换行符转换为一行,php,tags,meta,Php,Tags,Meta,我在mySQL中存储了带换行符的描述,所以当我按原样输出它们时,我使用: <? echo nl2br($description); ?> 到目前为止,一切顺利。现在,我想对meta标记使用相同的描述,问题是即使我像这样输出它们: $old_string = nl2br($description); $new_string = preg_replace("/<br \/>/"," ",$old_string); echo $new_string; 我仍然得到这些元标记:

我在mySQL中存储了带换行符的描述,所以当我按原样输出它们时,我使用:

<?
echo nl2br($description);
?>
到目前为止,一切顺利。现在,我想对meta标记使用相同的描述,问题是即使我像这样输出它们:

$old_string = nl2br($description);
$new_string = preg_replace("/<br \/>/"," ",$old_string);
echo $new_string;
我仍然得到这些元标记:

<meta property="og:description" content="Line 1

Line 2

Line 3"/>
我如何使它在一行中输出,它们之间只有一个空格

非常感谢您的帮助:

试试:

$singleLine = str_replace("\n", " ", $string);
如果没有nl2br,只需从数据库中获取字符串。

不要将nl2br用于META,只需替换数据库中字符串上的换行符即可。请注意,根据您的环境,换行符可以用\r\n或简单地用\r\n表示。因此,请尝试使用以下代码:

$onelinestring = str_replace("\r\n", " ", $description);
$onelinestring = str_replace("\n", " ", $onelinestring);

非常感谢你的回答。您的速度更快,但下面带有的\r\n实际上解决了我的问题。无论如何,谢谢您:冗余代码,只需为str_replace提供数组,而不是两次调用它:str_replaceArrayPp_EOL、\r\n、,$string@MichaelArenzon是的,可能有点多余,但它表示替换的顺序非常重要,不应该依赖底层框架实现来确保。
$onelinestring = str_replace("\r\n", " ", $description);
$onelinestring = str_replace("\n", " ", $onelinestring);