Reputation: 65
I'm using php to create a sitemap xml file for google submission but I'm getting an error in my code, which is:
<?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>
";
}
?>
When I run the page in my browser I get:
XML Parsing Error: not well-formed
The error seems to be with the &title=
section of the code but I really need that in there as this is how my url's look - How do I fix this error?
Upvotes: 1
Views: 346
Reputation: 27072
In XML ampersands has to be replaced by entity, use &
instead.
<loc>http://fulldistortion.co.uk/post.php?id=$post_id&title=$post_url_title</loc>
^^^^^
Upvotes: 2