我使用php创建一个sitemap 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解析错误:not well-formed
错误似乎是与代码的&title=
部分,但我真的需要在那里,因为这是如何我的url的外观-我如何修复这个错误?
在XML中,&号必须用entity代替,请使用&
。
<loc>http://fulldistortion.co.uk/post.php?id=$post_id&title=$post_url_title</loc>
^^^^^