0

I am trying to use $value inside the $feed_title variable. And generate all 200 $feed_title variables.

What I am trying to accomplish would look like this:

Feed Url: http://example.com/term/###/feed
Feed Title: Some Title

Where the ### varies from 100-300.

I am using the following code, and getting the urls, but not sure how to get the titles for each feed:

$arr = range(100,300); 

foreach ($arr as $key => $value) 
{ 
    unset($arr[$key + 1]);

    $feed_title = simplexml_load_file('http://www.example.com/term/'
     . ??? . '/0/feed');

    echo 'Feed URL: <a href="http://www.example.com/term/' . $value 
     . '/0/feed">http://www.example.com//term/' . $value 
     . '/0/feed</a><br/>  Feed Category: ' . $feed_title->channel[0]->title
     . '<br/>';
} 

Do I need another loop inside of the foreach?

2
  • 1
    This question is not clear. What are you still trying to do with the given URI? Commented Jul 10, 2013 at 1:08
  • Sorry for not being clear, I am trying to use $value inside the $feed_title variable. And generate all 200 $feed_title variables Commented Jul 10, 2013 at 1:12

2 Answers 2

1

If you want to get the title of a page, use this function:

    function getTitle($Url){
    $str = file_get_contents($Url);
    if(strlen($str)>0){
        preg_match("/\<title\>(.*)\<\/title\>/",$str,$title);
        return $title[1];
    }
}

Here's some sample code:

<?php
function getTitle($Url){
    $str = file_get_contents($Url);
    if(strlen($str)>0){
        preg_match("/\<title\>(.*)\<\/title\>/",$str,$title);
        return $title[1];
    }
}

$arr = range(300,305); 
foreach($arr as $value) 
{ 

    $feed_title = getTitle('http://www.translate.com/portuguese/feed/' . $value);

    echo 'Feed URL: <a href="http://www.translate.com/portuguese/feed/' . $value . '">http://www.translate.com/portuguese/feed/' . $value . '</a><br/>
          Feed Category: ' . $feed_title . '<br/>';


}
?>

This gets the title from translate.com pages. I just limited the number of pages for faster execution.

Just change the getTitle to your function if you want to get the title from xml.

Sign up to request clarification or add additional context in comments.

Comments

0

Instead of using an array created with range, use a for loop as follows:

for($i = 100; $i <= 300; $i++){
     $feed = simplexml_load_file('http://www.something.com/term/' . $i . '/0/feed');
     echo 'Feed URL: <a href="http://something.com/term/' . $i . '/0/feed">http://www.something.com/term/' . $i . '/0/feed/</a> <br /> Feed category: ' . $feed->channel[0]->title . '<br/>';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.