PHP站点地图解析错误

时间:2015-05-20 18:03:42

标签: php xml parsing sitemap

我正在使用php为谷歌提交创建一个站点地图xml文件,但我的代码中出现错误,即:

<?php
$get_posts_sql = "SELECT * FROM posts ORDER BY added DESC";
$get_posts_res = mysqli_query($con, $get_posts_sql);
while($post = mysqli_fetch_assoc($get_posts_res)){
    $post_id = $post["id"];
    $post_title = $post["title"];
    $post_added = $post["added"];
    $post_date =    date('Y-m-d', strtotime($post_added));

    $post_url_title = preg_replace('/[^a-zA-Z0-9_ %\[\]\.\(\)%&-]/s', '', $post_title);
    $post_url_title = strtolower(str_replace(" ","-",$post_url_title));

    $list_posts .= "

        <url>
            <loc>http://fulldistortion.co.uk/post.php?id=$post_id&title=$post_url_title</loc>
            <lastmod>$post_date</lastmod>
            <changefreq>monthly</changefreq>
            <priority>0.8</priority>
        </url>

    ";
}
?>

当我在浏览器中运行页面时,我得到:

  

XML解析错误:格式不正确

错误似乎与代码的&title=部分有关,但我真的需要在那里,因为这是我的网址的样子 - 如何修复此错误?

1 个答案:

答案 0 :(得分:2)

在XML中,&符号必须由实体替换,而是使用&amp;

<loc>http://fulldistortion.co.uk/post.php?id=$post_id&amp;title=$post_url_title</loc>
                                                     ^^^^^