-1

i want to create xml file with php ...

How can I jump line? with br or \ n?

I try it every time.

can anyone help me?

include "ayar.php";
$myFile = "rss.xml";
$fh = fopen($myFile, 'w') or die("can't open file");



$rss_txt .= '<?xml version="1.0"?>';
$rss_txt .= '<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">';
$rss_txt .= '<channel>';

 while($values_query = mysql_fetch_assoc($query))
 {
        $rss_txt .= '<item>';
        $rss_txt .= '<g:title>' .$values_query['baslik']. '</g:title><br />';
        $rss_txt .= '<g:description>' .$values_query['aciklama']. '</g:description><br />';
        $rss_txt .= '<g:link>' .$values_query['resim1k']. '</g:link><br />';
        $rss_txt .= '<g:image_link>' .$values_query['resim1k']. '</g:image_link><br />';    
        $rss_txt .= '</item>';

 }
$rss_txt .= '</channel>';
$rss_txt .= '</rss>';

fwrite($fh, $rss_txt);
fclose($fh);
Ken Ratanachai S.
  • 3,307
  • 34
  • 43
  • Have a quick read about [double-quoted strings](http://php.net/manual/en/language.types.string.php#language.types.string.syntax.double). – salathe Oct 03 '17 at 21:07

2 Answers2

0

An HTML line break sequence is definitely not right. I believe you need to use a 'carriage return' (CR) which could be written in your string as \r

RockwoodON
  • 149
  • 3
0

As RockwoodON did state correctly, a <br /> is not the way to go here. <br /> is a tag for HTML renderers to render a newline. To format your XML you will need line break characters/strings, which are unfortunately not platform independent.

Every platform has its line breaks

  • Windows hat Carriage Return plus linefeed ("\r\n")
  • *nix (excluding macOS) has a single linefeed "\n"
  • macOS has a single carriage return "\r"

Having said that, most modern editors might recognize several/all of those options. notepad++ does, VS too, just to mention two. So the choice of line breaks should depend on your target system/editor or use-case respectively.

If the system your code runs on is the same as the target platform, you might use PHP_EOL (see here), but it's only useful as long as you are not switching platforms on either side. But you should really think about defining you own constant, to stay consistent withing your file

define('EOL',   '\r\n');

// ...

$rss_txt .= '<item>';
$rss_txt .= '<g:title>' .$values_query['baslik']. '</g:title>'.EOL;
$rss_txt .= '<g:description>' .$values_query['aciklama']. '</g:description>'.EOL;
$rss_txt .= '<g:link>' .$values_query['resim1k']. '</g:link>'.EOL;
$rss_txt .= '<g:image_link>' .$values_query['resim1k']. '</g:image_link>'.EOL;    
$rss_txt .= '</item>';
Paul Kertscher
  • 9,416
  • 5
  • 32
  • 57