php中的数组到字符串用新行替换空格

时间:2017-01-22 14:02:31

标签: php

$reply[$c]包含" 123 123 123"我如何转换为$reply[$c]等于123\n123\n123

  

echo str_replace(''," \ n",爆炸(" \ 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
        }
}

完整代码参考:http://nearby.org.uk/sphinx/search-example5-sphinxql.phps

1 个答案:

答案 0 :(得分:0)

您不需要explode字符串。只需用换行符替换空格就足够了。

echo str_replace(' ', "\n", $reply[$c]);