php中的数组到字符串用新行替换空格
php中的数组到字符串用新行替换空格,php,Php,$reply[$c]包含“123”如何将$reply[$c]转换为等于123\n123\n123 echo str_replace(“”,“\n”,explode(“\n”,$reply[$c]) 我的代码失败了^ 下面的代码显示了$reply[$c]是如何形成和使用的,这对php来说是非常陌生的 快速代码参考: if (mysql_num_rows($result) > 0) { //Fetch Results from Mysql (Store in an accoc
$reply[$c]
包含“123”如何将$reply[$c]转换为等于123\n123\n123
echo str_replace(“”,“\n”,explode(“\n”,$reply[$c])
我的代码失败了^
下面的代码显示了$reply[$c]
是如何形成和使用的,这对php来说是非常陌生的
快速代码参考:
if (mysql_num_rows($result) > 0) {
//Fetch Results from Mysql (Store in an accociative array, because they wont be in the right order)
$rows = array();
while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$rows[$row['id']] = $row;
}
//Call Sphinxes BuildExcerpts function
if ($CONF['body'] == 'excerpt') {
$docs = array();
foreach ($ids as $c => $id) {
$docs[] = "'".mysql_real_escape_string(strip_tags($rows[$id]['body']))."'";
}
$result = mysql_query("CALL SNIPPETS((".implode(',',$docs)."),'{$CONF['sphinx_index']}','".mysql_real_escape_string($q)."')",$sphinxql);
$reply = array();
while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$reply[] = $row['snippet'];
}
}
foreach ($ids as $c => $id) {
$row = $rows[$id];
print ($reply[$c]); //Need to replace spaces with new lines here
}
}
完整代码参考:您不需要分解字符串。用换行符替换空格就足够了
echo str_replace(' ', "\n", $reply[$c]);
为什么要传递数组而不是字符串?“爆炸”的原因是:
$reply[$c]=echo str\u replace(“”\n“,$reply[$c])代码>我得到错误:PHP解析错误:语法错误,意外的“echo”(T_echo)忽略我在开始时尝试的内容如果你向下查看代码块底部,你会看到print($reply[$c])
这打印出123 123
我需要它打印出123\n123\n123
链接到我使用的代码模板,以供完整代码参考: