简体   繁体   中英

XML Sitemap Parsing Error With PHP

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?

In XML ampersands has to be replaced by entity, use &amp; instead.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM