Page 1 of 1

using substring in xml created by PHP

Posted: Tue Oct 13, 2009 9:47 am
by agebpt
Hi everyone, am currently a bit stuck.

Below I'm creating a very simple XML document using PHP, works fine normally. But I'm trying to create a character limit on one of the nodes, specifically the news_post row out of the database. I've read up and successfully implemented a character restriction just using php, but I can't seem to get that character limit to work with as printed out xml - is this possible?

Below is my current attempt, using acharacter position of 300 to cut off, and also implementing a very useful whitespace search that i found, so that the character limit doesn;t cut off in the middle of a word:

Code: Select all

 
<?php
include("db_connect.php");
loginInfo();
header('Content-type: text/xml');
?>
 
<?php echo "<?"; ?>xml version="1.0" encoding="UTF-8859-1" ?>
    
<?php
mysql_pconnect ($host, $user, $pass) or die ("Login info is incorrect") ; 
mysql_select_db ($database);
 
echo "<newsPosts>";
 
    $query = "SELECT * FROM $tableTwo ORDER BY id DESC";                
    $result = mysql_query($query) or die(mysql_error());
    
    $rStringPost = "";
    
    $position=300;
    
    $post = substr($rStringPost,$position,1); 
    
    if($post !=" "){
        while($post !=" "){
            $i=1;
            $position=$position+$i;
            
            $rStringPost = htmlspecialchars(stripslashes($row['news_post']));
            $post = substr($rStringPost,$position,1);
        }
    }
    
    $post = substr($rStringPost,0,$position);
    
    echo '<item>
    <title><![CDATA['.htmlspecialchars($row['news_title']).']]></title>
    <description><![CDATA['.htmlspecialchars(stripslashes($row['news_post'])).']]></description>
    <pubDate>'.$row['date'].'</pubDate>
    </item>';
    
    echo "</newsPosts>";
?>
 
Any advice would be excellent - i understand that the problem is the $post not being correct, but I'm not sure of how to implement it otherwise. This is the error i get from the xml document:
XML Parsing Error: XML or text declaration not at start of entity
Location: http://localhost:8888/flashSite/latest_news.php
Line Number 2, Column 1:<?xml version="1.0" encoding="UTF-8859-1" ?>
^(this arrow points under the initial <? bit)


Cheers

Re: using substring in xml created by PHP

Posted: Wed Oct 14, 2009 9:59 am
by agebpt
Hey all, does anyone have any solution or any idea to this?? Still trying to work it out, but can't come up with anything!
Cheers