tags:

views:

116

answers:

2
+1  Q: 

Rss Function

I have a function to build a rss feed with PHP:

    function createRSS() {
    $currentDate = time();
    $sql = "SELECT * FROM ". ADS_TABLE ." WHERE expires > $currentDate ORDER BY enterd DESC LIMIT 0,400";
    $results = myExec($sql);
    // open a file pointer to an RSS file
    $fp = fopen ("mexautosrss.xml", "w");
    if (!$fp) {
    // Can't write to a file
    return;
    }

    fwrite ($fp, "<?xml version='1.0' encoding='iso-8859-1' ?>\n");
    //ERROR:  fwrite ($fp, "<rss version='2.0' xmlns:atom="http://www.mexautos.com/mexautosrss.xml"&gt;&lt;channel&gt;\n");
    fwrite ($fp, "<title>MexAutos.com</title>\n");
    //ERROR:  fwrite ($fp, "<atom:link href="http://www.mexautos.com/mexautosrss.xml" rel="self" type="application/rss+xml" />\n");
    fwrite ($fp, "<link>http://www.mexautos.com/&lt;/link&gt;\n");
    fwrite ($fp, "<description>Anuncios de Autos y Camionetas Usados en Mexico.</description>\n");
    fwrite ($fp, "<language>es-mx</language>\n");
    fwrite ($fp, "<docs>http://www.mexautos.com/mexautosrss.xml&lt;/docs&gt;\n");
    fwrite ($fp, "<image>\n");
    fwrite ($fp, " <title>Mexautos.com</title>\n");
    fwrite ($fp, " <url>http://www.mexautos.com/images/logo_top_es.gif&lt;/url&gt;\n");
    fwrite ($fp, " <link>http://www.mexautos.com&lt;/link&gt;\n");
    fwrite ($fp, "</image>\n");

    while ($row = mysql_fetch_array($results)) {
    $makeId = $row['make'];
    $makeSQL = "SELECT name FROM ". CAR_MAKES_TABLE ." WHERE pkMakeID=$makeId";
    $makeResults = myExec($makeSQL);
    $make = mysql_fetch_row($makeResults);
    $modelId = $row['model'];
    $makeSQL = "SELECT name FROM ". CAR_MODELS_TABLE ." WHERE pkModelID=$modelId";
    $makeResults = myExec($makeSQL);
    $model = mysql_fetch_row($makeResults);
    $stateId = $row['state'];
    $makeSQL = "SELECT name FROM ". STATES_TABLE ." WHERE pkStateID=$stateId";
    $makeResults = myExec($makeSQL);
    $state = mysql_fetch_row($makeResults);
    $title = $make[0]." ".$row['make_other']." ".$model[0]." ".$row['model_other']." '".$row['model_year'];
    $content = "$".$row['price']." mil pesos ".$row['color']." Clima: ".$row['clima']." ".$row['milage']." mil kms Puertas: ".$row['doors']." ".$row['transmission']." ".$row['comment']." Tel: ".$row['tel_num']." (".$state[0].")";
    $search = array(
    '@<script[^>]*?>.*?</script>@si', // Strip out javascript
    '@<[\/\!]*?[^<>]*?>@si', // Strip out HTML tags
    '@([\r\n])[\s]+@', // Strip out white space
    '@&(quot|#34);@i', // Replace HTML entities
    '@&(amp|#38);@i',
    '@&(lt|#60);@i',
    '@&(gt|#62);@i',
    '@&(nbsp|#160);@i',
    '@&(iexcl|#161);@i',
    '@&(cent|#162);@i',
    '@&(pound|#163);@i',
    '@&(copy|#169);@i',
    '@&(acento|#0027);@i',
    '@&#(\d+);@e'); // evaluate as php
    $replace = array(
    '',
    '',
    '\1',
    '"',
    '&',
    '<',
    '>',
    ' ',
    'chr(161)',
    'chr(162)',
    'chr(163)',
    'chr(169)',
    'chr(\1)');
    $content = preg_replace($search, $replace, $content);
    $title = preg_replace("/&/", 'y', $title);
    $content = preg_replace("/&/", 'y', $content);
    fwrite ($fp, "<item>\n");
    fwrite ($fp, " <title>$title</title>\n");
    fwrite ($fp, " <description>$content</description>\n");
    fwrite ($fp, " <link>http://www.mexautos.com/&lt;/link&gt;\n");
    fwrite ($fp, "<guid>http://www.mexautos.com&lt;/guid&gt;\n");
    fwrite ($fp, "</item>\n");
    }
    fwrite ($fp, "</channel></rss>\n");
    fclose ($fp);
    }

The problem is that I get an error if I do not comment out the "atom:link" lines, it shows:

Parse error: syntax error, unexpected T_STRING

Could someone point me where its the error?

Thx

+1  A: 

If you contain double-quote characters inside a double-quoted string in PHP (or just about any other language), you need to escape those literal double-quote characters.

fwrite ($fp, "<rss version='2.0' xmlns:atom=\"http://www.mexautos.com/mexautosrss.xml\"&gt;&lt;channel&gt;\n");

Or else use single-quotes, which are typically interchangeable in HTML:

fwrite ($fp, "<rss version='2.0' xmlns:atom='http://www.mexautos.com/mexautosrss.xml'&gt;&lt;channel&gt;\n");

See specification of quote types to delimit HTML/SGML attributes here: http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.2.2

By default, SGML requires that all attribute values be delimited using either double quotation marks (ASCII decimal 34) or single quotation marks (ASCII decimal 39). Single quote marks can be included within the attribute value when the value is delimited by double quote marks, and vice versa.

The last sentence applies to HTML attribute strings that contain quotes, which is not your scenario. But you do have PHP code with a quoted string containing quotes, and PHP supports the same policy about quotes.

Bill Karwin
Actually this is XML, not HTML/SGML, but the same applies: XML allows either kind of quote marks to delimit attributes.
David Zaslavsky
+1  A: 

You need to correctly escape strings:

fwrite ($fp, "<rss version='2.0' xmlns:atom=\"http://www.mexautos.com/mexautosrss.xml\"&gt;&lt;channel&gt;\n");
mpeterson