I want to create a feed for a several pinterest boards. Before I get down too deep, I see the images, but they are small.
Before, using yahoo pipes I was able to modify the source and change 236x with 736x in the image URL and recreated the feed. But now that the service is about to shut down, I want to find a different alternative. Tried several services, but none seem to do the replacement as I want.
I know the image is in the description, but can't figure out how to use str_replace to accomplish this.
Hope you can share some ideas, thank you.
I have this so far:
<?php
$rss = new DOMDocument();
$feed = array();
$urlArray = array(array('name' => 'Otaku', 'url' => 'https://www.pinterest.com/anizeen/otaku.rss'),
array('name' => 'How To', 'url' => 'https://www.pinterest.com/anizeen/how-to-draw-anime-manga.rss'),
array('name' => 'Upcoming', 'url' => 'https://www.pinterest.com/anizeen/upcoming-anime-attractions.rss'),
array('name' => 'Manga', 'url' => 'https://www.pinterest.com/anizeen/manga.rss')
);
foreach ($urlArray as $url) {
$rss->load($url['url']);
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'site' => $url['name'],
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
array_push($feed, $item);
}
}
usort($feed, function($a, $b) {
return strtotime($b['date']) - strtotime($a['date']);
});
$limit = 3;
echo '<ul>';
for ($x = 0; $x < $limit; $x++) {
$site = $feed[$x]['site'];
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$desc = str_replace('/236x/', '/736x/', $desc);
$description = $feed[$x]['desc'];
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<p><strong>'.$title.'</strong><br />';
echo '<small><em>'.$date.'</em></small></p>';
echo '<p>'.$description.'</p>';
}
?>
Related
I have some code that takes an rss feed url, expands the rss link and gets the individual links inside that feed xml. After checking if a link exists, i insert it to a table if it does not exist and do nothing if it does. However my code is becoming more unreadable and one more check that requires another foreach it shall be even more unreadable.
This is the code
public function links_cron_job(){
//Get Rss Links
$this->db->select("the_link_itself");
$query = $this->db->get_where("search_engine_links", array("link_type" => "rss"));
foreach ($query->result() as $row){
$url = $row->the_link_itself;
$rss = Feed::loadRss($url);
foreach ($rss->item as $item) {
$this->db->where('the_link_itself',$item->link);
$query3 = $this->db->get('search_engine_links');
if ($query3->num_rows() > 0){
echo 'duplicates are there';
}else{
$data = array(
'link_country' => 'usa',
'the_link_itself' => $item->link,
'link_category' => 'news_website',
'link_added_by' => 'admin',
'link_type' => 'ordinary_link',
'link_local_type' => 'news',
'link_region' => 'countrywide',
'link_city' => 'washington',
'date_added' => $item->timestamp,
'last_updated' => time()
);
$this->db->insert('search_engine_links', $data);
echo 'no duplicates are there';
}
}
}
}
What would be another approach in doing what i am doing?
Normally I would say, just enter a return. But in this case you still might have
work to do in the extra iterations. So in this case at least we know that we are
done with this iteration, so at least we can add a continue statement:
<?php
foreach ($rss->item as $item) {
$this->db->where('the_link_itself',$item->link);
$query3 = $this->db->get('search_engine_links');
if ($query3->num_rows() > 0) {
echo 'duplicates are there';
continue;
}
$data = array(
'link_country' => 'usa',
'the_link_itself' => $item->link,
'link_category' => 'news_website',
'link_added_by' => 'admin',
'link_type' => 'ordinary_link',
'link_local_type' => 'news',
'link_region' => 'countrywide',
'link_city' => 'washington',
'date_added' => $item->timestamp,
'last_updated' => time()
);
$this->db->insert('search_engine_links', $data);
echo 'no duplicates are there';
}
This is my simple looper code
foreach( $cloud as $item ) {
if ($item['tagname'] == 'nicetag') {
echo $item['tagname'];
foreach( $cloud as $item ) {
echo $item['desc'].'-'.$item['date'];
}
} else
//...
}
I need to use if method in this looper to get tags with same names but diferent descriptions and dates. The problem is that I dont know every tag name becouse any user is allowed to create this tags.
Im not really php developer so I'm sory if it's to dummies question and thanks for any answers!
One possible solution is to declare a temporary variable that will hold tagname that is currently looped through:
$currentTagName = '';
foreach( $cloud as $item ) {
if ($item['tagname'] != $currentTagName) {
echo $item['tagname'];
$currentTagName = $item['tagname'];
}
echo $item['desc'] . '-' . $item['date'];
}
I presume that your array structure is as follows:
$cloud array(
array('tagname' => 'tag', 'desc' => 'the_desc', 'date' => 'the_date'),
array('tagname' => 'tag', 'desc' => 'the_desc_2', 'date' => 'the_date_2'),
...
);
BUT
This solution raises a problem - if your array is not sorted by a tagname, you might get duplicate tagnames.
So the better solution would be to redefine your array structure like this:
$cloud array(
'tagname' => array (
array('desc' => 'the_desc', 'date' => 'the_date'),
array('desc' => 'the_desc_2', 'date' => 'the_date_2')
),
'another_tagname' => array (
array('desc' => 'the_desc_3', 'date' => 'the_date_3'),
...
)
);
and then you can get the data like this:
foreach ($cloud as $tagname => $items) {
echo $tagname;
foreach($items as $item) {
echo $item['desc'] . '-' . $item['date'];
}
}
I have an array in $xml->channel->item which outputs correctly if I run:
foreach ($xml->channel->item as $entry){
echo $entry->title;
echo $entry->category;
echo $entry->pubDate;
}
Now, I am struggling to make this array work with the following form, which outputs a formatted table:
$rows = array(
'row[0]' => array('title' => 'Test Title','category' => 'Computer', 'date' => 'test date'),
'row[1]' => array('title' => 'Test Title 2','category' => 'Chemical', 'date' => 'test date'),
);
$form['table'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $rows,
);
I tried this code, but it didn't work:
$i=0;
foreach ($xml->channel->item as $rows){
row[$i][title] = $rows->title;
row[$i][category] = $rows->category;
row[$i][date] = $rows->pubDate;
$i=++;
}
SOLUTION:
Copying the solution I figured out from the comment below to the post body:
$rows = array();
foreach ($xml->channel->item as $entry) {
$row['title'] = $entry->title;
$row['category'] = $entry->category;
$row['date'] = $entry->pubDate;
array_push($rows, $row);
}
Finally figured out. I hope this will save others hitting the similar task:
$rows = array();
foreach ($xml->channel->item as $entry) {
$row['title'] = $entry->title;
$row['category'] = $entry->category;
$row['date'] = $entry->pubDate;
array_push($rows, $row);
}
I don't actually have a problem, however, I am looking to improve my source code. I have three different queries whose results should be combined in an array. An example of the array:
array(
array(
'q0-result' => '...',
'q1-result' => '...',
'q2-result' => '...'
),
array(
'q0-result' => '...',
'q1-result' => '...',
'q2-result' => '...'
)
);
So now I have the following source code, however, I do not like the usage of $idx and how the array is being built. Do you have suggestions to improve the following source code?:
$data = array();
$dom = new DomDocument();
#$dom->loadHTML(file_get_contents('http://www.example.com'));
$xpath = new DomXpath($dom);
$idx = 0;
foreach($xpath->query('query0') as $element) {
$data[$idx]['q0-result'] = $element->nodeValue;
}
$idx = 0;
foreach($xpath->query('query1') as $element) {
$data[$idx]['q1-result'] = $element->nodeValue;
}
$idx = 0;
foreach($xpath->query('query2') as $element) {
$data[$idx]['q2-result'] = $element->nodeValue;
}
var_dump($data);
(If i understood) Do it:
foreach($xpath->query('query0') as $element)
$data['q0-result'][] = $element->nodeValue;
foreach($xpath->query('query1') as $element)
$data['q1-result'][] = $element->nodeValue;
foreach($xpath->query('query2') as $element)
$data['q2-result'][] = $element->nodeValue;
// below chunking in your format
/*
array(
array(
'q0-result' => '...',
'q1-result' => '...',
'q2-result' => '...'
),
array(
'q0-result' => '...',
'q1-result' => '...',
. ......
*/
$out = array();
$row0=array_shift($data['q0-result']);
$row1=array_shift($data['q1-result']);
$row2=array_shift($data['q2-result']);
while($row0 || $row1 || $row2){
$row0 = !empty($row0)? $row0 : null;
$row1 = !empty($row1)? $row1 : null;
$row2 = !empty($row2)? $row2 : null;
$out[] = array('q0-result'=>$row0,'q1-result'=>$row1,'q2-result'=>$row2 );
$row0=array_shift($data['q0-result']);
$row1=array_shift($data['q1-result']);
$row2=array_shift($data['q2-result']);
}
echo '<pre>';
print_r($out);
echo '</pre>';
I am trying to add some html code before each tag printed as an array element.
My code:
$term_links = array();
foreach ($vars['node']->taxonomy as $term)
{
$term_links[] = l($term->name, 'taxonomy/term/' . $term->tid,
array(
'attributes' => array(
'title' => $term->description
)));
}
$vars['node_terms'] = implode(', ', $term_links);
At the moment the tags are printed seperated by a comma. I would like to add a small image before each tag element using img src="tag.png" How can I do this?
EDIT - My current Code, still not working.
if (module_exists('taxonomy')) {
$img = 'some html';
$text = $img . $term->name;
$path = 'taxonomy/term/' . $term->tid;
$term_links = array();
foreach ($vars['node']->taxonomy as $term) {
$term_links[] = l($text, $path, array(
'html' => TRUE,
'attributes' => array(
'title' => $term->description
)));
}
$vars['node_terms'] = implode(', ', $term_links);
}
}
Dupal's l() function has an option "html" you can set it to TRUE and use IMG + TITLE as a title.
Here is the example:
$img = '<img src="..." />';
$text = $img . $term->name;
$path = 'taxonomy/term/' . $term->tid;
$term_links[] = l($text, $path, array(
'html' => TRUE,
'attributes' => array(
'title' => $term->description
)
));