Jump to content

php array variable question...


cowboysdude
 Share

Go to solution Solved by requinix,

Recommended Posts

I'm currently parsing multiple rss/xml feeds using this:

class Feed_Amalgamator
{
    public $urls = array();
    public $data = array();

    public function addFeeds( array $feeds )
    {
        $this->urls = array_merge( $this->urls, array_values($feeds) );
    }

    public function grabRss()
    {
        foreach ( $this->urls as $feed )
        {
            $data = @new SimpleXMLElement( $feed, 0, true );
            if ( !$data )
                throw new Exception( 'Could not load: ' . $feed );
            foreach ( $data->channel->item as $item )
            {
                $this->data[] = $item;
            }
        }
    }

    public function amalgamate()
    {
        shuffle( $this->data );
        $temp = array();
        foreach ( $this->data as $item )
        {
            if ( !in_array($item->link, $this->links($temp)) )
            {
                $temp[] = $item;
            }
        }
        $this->data = $temp;
        shuffle( $this->data );
    }

    private function links( array $items )
    {
        $links = array();
        foreach ( $items as $item )
        {
            $links[] = $item->link;
        }
        return $links;
    }
}

/********* Feeds *********/
$urls = array('http://www.dallascowboys.com/rss/video',
'http://www.dallascowboys.com/rss/gallery',
'http://www.dallascowboys.com/rss/audio',
'http://espn.go.com/blog/feed?blog=nfceast',
'https://sports.yahoo.com/nfl/teams/dal/rss.xml',);
/********* Feeds *********/
try
{
    $feeds = new Feed_Amalgamator;
    $feeds->addFeeds( $urls );
    $feeds->grabRss();
    $feeds->amalgamate();
}
catch ( exception $e )
{
    die( $e->getMessage() );
}

foreach ( $feeds->data as $item ) :
extract( (array) $item );

$truncated = (strlen($description) > 100) ? substr($description, 0, 80) . '...' : $description;

and it's working quite nicely... however I'm trying to replace the actual feeds them selves in this section:

/********* Feeds *********/
$urls = array('http://www.dallascowboys.com/rss/video',
'http://www.dallascowboys.com/rss/gallery',
'http://www.dallascowboys.com/rss/audio',
'http://espn.go.com/blog/feed?blog=nfceast',
'https://sports.yahoo.com/nfl/teams/dal/rss.xml',);
/********* Feeds *********/

 with this:

/********* Feeds *********/
$urls = array($params->get('url'));
/********* Feeds *********/

But I keep getting this:

 

  • String could not be parsed as XML

I know it's reading the links but it doesn't seem to be passing them correctly to be parsed for some reason...

 

Suggestions?

 

Thanks in advance!

Link to comment
Share on other sites

Well, first step is figuring out the exact value of $urls...

print_r($urls);

 

 

This the output:

Array
(
    [0] => http://www.dallascowboys.com/rss/video
    [1] => http://www.dallascowboys.com/rss/gallery
    [2] => http://www.dallascowboys.com/rss/audio
    [3] => http://espn.go.com/blog/feed?blog=nfceast
    [4] => https://sports.yahoo.com/nfl/teams/dal/rss.xml
) 
Edited by cowboysdude
Link to comment
Share on other sites

When I use the 

print_r($url);


it returns

'http://www.dallascowboys.com/rss/video',
'http://www.dallascowboys.com/rss/gallery',
'http://www.dallascowboys.com/rss/audio',
'http://espn.go.com/blog/feed?blog=nfceast',
'https://sports.yahoo.com/nfl/teams/dal/rss.xml',<div id="wrapper" style="margin-top:1px;"><ul id="ticker">String could not be parsed as XML
Link to comment
Share on other sites

Here's the thing: with

/********* Feeds *********/
$urls = array($params->get('url'));
/********* Feeds *********/
$urls is guaranteed to be an array containing one single item. That contradicts the first output you gave.

 

As for the second, there is no "$url" in the code you posted so I don't know where its value is coming from.

Link to comment
Share on other sites

Here is the entire code... I see what you mean...

JHtml::stylesheet('modules/mod_newsbanner/assets/css/styles.css');
JHtml::_('jquery.framework');
JHtml::script(JUri::base() . 'modules/mod_newsbanner/assets/js/tickerme.js');
$url  = $params->get('url');
 echo '<div id="wrapper" style="margin-top:1px;">';
 echo '<ul id="ticker">';

class Feed_Amalgamator
{
    public $urls = array();
    public $data = array();

    public function addFeeds( array $feeds )
    {
        $this->urls = array_merge( $this->urls, array_values($feeds) );
    }

    public function grabRss()
    {
        foreach ( $this->urls as $feed )
        {
            $data = @new SimpleXMLElement( $feed, 0, true );
            if ( !$data )
                throw new Exception( 'Could not load: ' . $feed );
            foreach ( $data->channel->item as $item )
            {
                $this->data[] = $item;
            }
        }
    }

    public function amalgamate()
    {
        shuffle( $this->data );
        $temp = array();
        foreach ( $this->data as $item )
        {
            if ( !in_array($item->link, $this->links($temp)) )
            {
                $temp[] = $item;
            }
        }
        $this->data = $temp;
        shuffle( $this->data );
    }

    private function links( array $items )
    {
        $links = array();
        foreach ( $items as $item )
        {
            $links[] = $item->link;
        }
        return $links;
    }
}

/********* Feeds *********/
$urls = array($url);
/********* Feeds *********/

try
{
    $feeds = new Feed_Amalgamator;
    $feeds->addFeeds( $urls );
    $feeds->grabRss();
    $feeds->amalgamate();
}
catch ( exception $e )
{
    die( $e->getMessage() );
}

foreach ( $feeds->data as $item ) :
extract( (array) $item );

$truncated = (strlen($description) > 100) ? substr($description, 0, 80) . '...' : $description;



?>

<li><img src="modules/mod_newsbanner/assets/images/slogo.jpg" width="19" height="19">   <?php echo $truncated; ?>  <a href="<?php echo $link; ?>" target="_blank"><font color=red>Read More</font></a></li>

<?php endforeach; ?>
</ul>
  </div>
  
  <script>
jQuery(function(){
		jQuery('#ticker').tickerme();
	});
</script>
Edited by cowboysdude
Link to comment
Share on other sites

Assuming you had

$url  = $params->get('url');
 print_r($url);
 echo '<div id="wrapper" style="margin-top:1px;">';
 echo '<ul id="ticker">';
that means $url is actually the string

'http://www.dallascowboys.com/rss/video',
'http://www.dallascowboys.com/rss/gallery',
'http://www.dallascowboys.com/rss/audio',
'http://espn.go.com/blog/feed?blog=nfceast',
'https://sports.yahoo.com/nfl/teams/dal/rss.xml',
which is no good. How is the "url" parameter being set? Can you make it an array of strings?
Link to comment
Share on other sites

Well if you go back up to the top I have set $url to get the 

'http://www.dallascowboys.com/rss/video',
'http://www.dallascowboys.com/rss/gallery',
'http://www.dallascowboys.com/rss/audio',
'http://espn.go.com/blog/feed?blog=nfceast',
'https://sports.yahoo.com/nfl/teams/dal/rss.xml',

which comes from:


/********* Feeds *********/
$urls = array($url);
/********* Feeds *********/

it should be getting 'filled' from this:

$url  = $params->get('url');

 which is a 'textarea' where users can put their urls like above and populate the array via the $url parameter.. 

 

It's not reading it for some strange reason... the 

print_r($url); 

was just to see if that $url was catching what was being put in the textarea in the module and it is.... problem is it's not translating later on down in the array() area....

Link to comment
Share on other sites

  • Solution

which is a 'textarea' where users can put their urls like above and populate the array via the $url parameter..

And there it is.

 

See, the "url" param, and thus the $url variable, and thus the $urls array, contains a string of all the URLs. That's no good. Your code needs an array of individual URLs, not all of them bunched together.

 

1. You copied and pasted that

'http://www.dallascowboys.com/rss/video',
'http://www.dallascowboys.com/rss/gallery',
'http://www.dallascowboys.com/rss/audio',
'http://espn.go.com/blog/feed?blog=nfceast',
'https://sports.yahoo.com/nfl/teams/dal/rss.xml',
into the textarea. You, as a user, need to fix that so it becomes

http://www.dallascowboys.com/rss/video
http://www.dallascowboys.com/rss/gallery
http://www.dallascowboys.com/rss/audio
http://espn.go.com/blog/feed?blog=nfceast
https://sports.yahoo.com/nfl/teams/dal/rss.xml
One URL for each line. No extra apostrophes or commas or anything. Make sure your form has instructions to do that.

 

2. Now you know each line is a URL. Supposedly. IMO the best way to get the individual URLs out of it is to try to locate a valid URL on each line, which gives you some freedom about leaving blank lines and such:

$url = $params->get('url');
// beginning of line, maybe whitespace, a "http://" or "https://" url, maybe more whitespace, then the end of the line
if (preg_match_all('/^\s*(https?://\S+)\s*$/im', $url, $matches, PREG_PATTERN_ORDER)) {
	$urls = $matches[1];
} else {
	// did not find any urls
	$urls = array();
}
3. Now that you have $urls set you don't need

/********* Feeds *********/
$urls = array($url);
/********* Feeds *********/
Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.