Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6794073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:10:40+00:00 2026-05-26T18:10:40+00:00

I have a little php/mysql app I put together that takes an input form

  • 0

I have a little php/mysql app I put together that takes an input form and stores it in a MySQL database, and outputs the data as XML for consumption by a radio-playing hardware device.

The problem is ampersands and other characters. The user is taking descriptions of various radio stations, along with streaming URL or Playlist URL and pasting them into the form. Some radio stations are in non-english speaking countries (mostly French). I need to know what to do to preprocess these fields so that the XML that is generated is not corrupted, which breaks the external hardware app.

I assume that this should go into the php that is called when the form is submitted. I’m pretty sure the htmlspecialchars function should be used, but I’m not sure the best method, since I’ve hacked this together from a variety of sources:

UPDATE: Here is my current output code with some regex that cleans up the ampersands.

<?
include("HLN/manager/connect.php");

$query = "SELECT * FROM hln_stations ORDER BY orderid ASC";
$result = mysql_query($query);

$num = mysql_num_rows ($result);
mysql_close();

$xml = new XMLWriter();

$xml->openURI("php://output");
$xml->startDocument();
header('Content-type: text/xml');
$xml->setIndent(true);

$xml->startElement('channels');

while ($row = mysql_fetch_assoc($result)) {

  $xml->startElement("channel");
     $xml->startElement("title");
          $xml->writeRaw(preg_replace('/&(?![A-Za-z0-9#]{1,7};)/','&amp;',$row['station_title']));
     $xml->endElement();
     $xml->startElement("descriptionline1");
          $xml->writeRaw(preg_replace('/&(?![A-Za-z0-9#]{1,7};)/','&amp;',$row['station_display_name']));
     $xml->endElement();

     $xml->startElement("descriptionline2");
          $xml->writeRaw(preg_replace('/&(?![A-Za-z0-9#]{1,7};)/','&amp;',$row['station_subtitle']));
     $xml->endElement();

     $xml->startElement("description");
          $xml->writeRaw(preg_replace('/&(?![A-Za-z0-9#]{1,7};)/','&amp;',$row['station_detailed_description']));
     $xml->endElement();

     $xml->startElement("sdimage");
          $xml->writeRaw(preg_replace('/&(?![A-Za-z0-9#]{1,7};)/','&amp;',$row['sdtv_thumbnail_graphic_url']));
     $xml->endElement();

     $xml->startElement("hdimage");
          $xml->writeRaw(preg_replace('/&(?![A-Za-z0-9#]{1,7};)/','&amp;',$row['hdtv_thumbnail_graphic_url']));
     $xml->endElement();

     $xml->startElement("uri");
          $xml->writeRaw(preg_replace('/&(?![A-Za-z0-9#]{1,7};)/','&amp;',$row['stream_url_or_playlist_url']));
     $xml->endElement();

     $xml->startElement("linktype");
          $xml->writeRaw(preg_replace('/&(?![A-Za-z0-9#]{1,7};)/','&amp;',$row['link_type']));
     $xml->endElement();

 $xml->endElement();
}

$xml->endElement();


$xml->flush();

?>

But I still need to solve the French character set issues that are cropping up. How can I replace the é character for example with something that doesn’t cause problems?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:10:41+00:00Added an answer on May 26, 2026 at 6:10 pm

    You’ve an error in Firefox, that says not well formed, because the character set detected doesn’t match the character set you output. I tried various combinations of character sets and could reproduce the issue.

    You’ve to specify explicitly your character sets, such as:

    header('Content-type: text/xml; charset=UTF-8');
    $xml = new XMLWriter();
    $xml->openURI("php://output");
    $xml->startDocument("1.0", "UTF-8");
    

    If specifying character set as UTF-8 in the content type and in XML gives you error, it means that your input is not valid UTF-8, try with ISO-8859-15 instead, or recode your input.

    You have to put the content-type charset header for every page of your site, including the form to input data or your special characters could be messed up. Further you’ve to connect to mysql specifying the character set that you want to use for the connection and that should match the charset and collation of your tables.

    Supposing that you’re using UTF-8 look at your database with PHPMyAdmin and a UTF-8 connection, if you can’t see your special characters well it means you’re doing something wrong.

    As for the device, if you say that it can display only ASCII characters, does it do the conversion for you when you give UTF-8 input or do you have to give the entity such as:

    Ch&#xE9;rie 
    

    If those two options doesn’t work, you may want to convert to ASCII, such as “Cherie”… but that would be the last resort.


    Proof of concept code without using the DB:

    <?php
    
    header('Content-type: text/xml; charset=UTF-8');
    
    $radioArr = array(
       array("Chérie FM @Work", "http://www.listenlive.eu/cheriefm_atwork.m3u?p&test"), 
       array("Hélène FM", "http://broadcast.infomaniak.ch/helenefm-high.mp3.m3u")
    );
    $xml = new XMLWriter();
    $xml->openURI("php://output");
    $xml->startDocument("1.0", "UTF-8");
    $xml->setIndent(true);
    $xml->startElement('channels');
    foreach ($radioArr AS $radio) {
         $xml->startElement("channel");
    
         $xml->startElement("title");
         $xml->writeRaw(preg_replace('/&(?![A-Za-z0-9#]{1,7};)/','&amp;', $radio[0]));
         $xml->endElement();
    
         $xml->startElement("uri");
         $xml->writeRaw(preg_replace('/&(?![A-Za-z0-9#]{1,7};)/','&amp;', $radio[1]));
         $xml->endElement();
    
         $xml->endElement(); //end channel
    }
    
    $xml->endElement();
    $xml->flush();
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an existing PHP MySQL web app that I want to make an
i have little problem with boost::asio library. My app receive and process data asynchronously,
I'm make a little game in php with mysql. Now I have a problem
I have a webapp written in PHP using a MySQL database backend. This question
I am designing a web application using php and mysql. I have a little
I have this little function function makewindows(){ child1 = window.open (about:blank); child1.document.write(<?php echo htmlspecialchars(json_encode($row2['ARTICLE_DESC']),
Our website code is written in PHP. We have very little testing in regards
This is a little tricky so bear with me: I have a PHP script
its a little bit hard to understand. in the header.php i have this code:
I have little snippet for validatin' my form. I need help to position the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.