dynamic sitemap showing error php xmlParseEntityRef: no name

Issue

I am making a dynamic sitemap and getting this error but, I am not understanding what is the problem. Please guide. I know some answer is already there

 error on line 87 at column 64: xmlParseEntityRef: no name

Here is the code

<?php
    require_once("configure/config.php");
    
    $status=1;
    $sql1 = "SELECT * FROM admitcard  WHERE status=? ORDER BY id DESC";
    $stmt1 = $mysqli->prepare($sql1); 
    $stmt1->bind_param("s", $status);
    $stmt1->execute();
    $admit_result = $stmt1->get_result();
    
    ?>
    <?php
    
    header("Content-Type: application/xml; charset=utf-8");
    
    echo '<!--?xml version="1.0" encoding="UTF-8"?-->'.PHP_EOL; 
    
    echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">' . PHP_EOL;
    echo '<url>' . PHP_EOL;
    echo '<loc>'.$base_url.'</loc>' . PHP_EOL;
    echo '<changefreq>daily</changefreq>' . PHP_EOL;
    echo '</url>' . PHP_EOL;
    
      while($row2=mysqli_fetch_array($admit_result)){
          echo '<url>' . PHP_EOL;
          echo '<loc>'.'localhost:8080/mysite/admitcard/'.$row2["id"].'/'.str_replace(' ','-',trim($row2['pagename_admit'])).'</loc>' . PHP_EOL;
          echo '<lastmod>'.date('c',time()).'</lastmod>' . PHP_EOL;
          echo '<changefreq>daily</changefreq>' . PHP_EOL;
          echo '</url>' . PHP_EOL;
         }
    
       echo '</urlset>' . PHP_EOL;
    ?>

Solution

You need to replace & with &amp;:

echo '<loc>'.'localhost:8080/mysite/admitcard/'.$row2["id"].'/'.str_replace(' ','-',trim(preg_replace('/&(?!#?[a-z0-9]+;)/', '&amp;', $row2['pagename_admit']))).'</loc>' . PHP_EOL;

Answered By – Vlad Salabun

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published