I am almost completely on my own trying to solve this problem, know next to nothing about PHP and am in desperate need of help. A Wordpress news site redesign has images included in the description tag of the feed, we already had a way set up to pull those images so now the feed on other sites is seeing double. I want to turn off the image being pulled in the description tag and I'm having trouble figuring out how to accomplish that.
I have tried a few things, like strip_tag, shuffling code/HTML, etc., but I suspect most of my problem is not knowing where or how to implement them.
if ($rss = simplexml_load_file($feed)){ // (replace relative path with variable: $rssPath ).
$count = 0;
foreach($rss->channel->item as $item){
if ($count < $items) {
/*Set variables from RSS*/
$title = $item->title;
$desc = $item->description;
$content = htmlspecialchars_decode($item->children('content', true)->encoded);
preg_match('/(<img[^>]+>)/i', $content,
$content_image);
//if(strstr($content, 'img')){
// $content_trunc =
substr($content,0,strpos($content,'</p>', 550)) . "...";
//}
//else {
//$content_trunc = substr($content,0,strpos($content,'</p>', 400)) .
"...";
//}
$content_image = str_replace('class="',
'class="media-object ', $content_image);
$link = $item->link;
$pubdate = $item->pubDate;
$pubMonth = substr($pubdate, 8, 3);
$pubDay = substr($pubdate, 5, 2);
/*Display items*/
$output= "<div class='row'>\n<div class='col-md-3'>";
$output = $output .
" <a href='". $link ."'>"
. $content_image[0] .
" </div>
<div class='col-md-9'><span class='news-headline' style='vertical-align:top;'>" . $title . "</span>
</a>" . "\n
<p class='news-blurb'>" . $desc . "</p>
</div>\n</div>\n <hr />";
echo $output;
$count++;
}
}
This is the description tag, in short:
<description><![CDATA[<img width="150" height="150" src="http://placehold.it/150.150" class="attachment-thumbnail size-thumbnail wp-post-image" alt="" />news article description here [...]]]></description>
Please help! This is a language I want to learn, but I'm a long way from it yet. All I know is that I need to filter content out of $desc. but I don't know how.
Try preg_replace("/<img[^>]+\>/i", "", $desc); after $desc = $item->description; line.
Related
Im trying to grab titles from http://alsat-m.tv/category/5/nga-vendi
but I cant. I have tried with below code. If any one can help me please?Below, I have added to pull titles only like a text and link.This code is working only with http://www.programminghelp.com/ and not with the other web pages, I dont know where the problem is.
<?php
$html = file_get_contents("http://alsat-m.tv/");
preg_match_all(
'/<h5><a href="(.*?)" rel="bookmark" title=".*?">(.*?)<\/a><\/h5>/s',
$html,
$posts, // will contain the article data
PREG_SET_ORDER // formats data into an array of posts
);
foreach ($posts as $post) {
$link = $post[1];
$title = $post[2];
echo "<a href'" . $link . "'>" . $title . "</a></br>";
}
echo "<p>" . count($posts) . " posts found</p>";
$html = file_get_contents("http://www.alsat-m.tv/");
preg_match_all(
'/<h5><a href="(.*?)" rel="bookmark" title=".*?">(.*?)<\/a><\/h5>/s',
$html,
$posts, // will contain the article data
PREG_SET_ORDER // formats data into an array of posts
);
foreach ($posts as $post) {
$link = $post[1];
$title = $post[2];
echo "<a href='" . $link . "'>" . $title . "</a></br>";
}
echo "<p>" . count($posts) . " posts found</p>";
?>
Here is a solution in python
import requests
from lxml import etree
xml = requests.get('http://alsat-m.tv/RssFeed')
tree = etree.fromstring(xml.content)
root = tree.find('channel')
titles = [x.find('title').text for x in root.findall('item')]
print titles
I'm getting category from my database using PHP and I want to use it inside Google Analytics event tracking code. The problem is that events are not being recorded in Google Analytics.
Here are some code snippets from my project:
1)
$docs = array();
while ($row = mysql_fetch_assoc($result)) {
$doc = array();
$doc['my_tel'] = $row['my_tel'];
$doc['my_category'] = $row['my_category'];
$docs[] = $doc;
}
mysql_free_result($result);
2)
<?php
$html_output = '';
foreach ($docs as &$d) {
$html_output .= '<div>' .
'<img src="call.png" width="65px" height="35px" border="0">' .
'</div>';
}
echo $html_output;
?>
If you have a look at the console of your browser there might be a JavaScript error, because the second parameter of _gaq.push is not wrapped with quotes.
Try this:
<?php
$html_output = '';
foreach ($docs as &$d) {
$html_output .= '<div>' .
'<img src="call.png" width="65px" height="35px" border="0">' .
'</div>';
}
echo $html_output;
?>
Looking at your append to $html_output, it appears you're not enclosing the category within single quotes in the resultant javascript output.
Assuming for example my_tel was 01234567890 and category was 'Category 1' the output from the code above would be:
<div><img src="call.png" width="65px" height="35px" border="0"></div>
Perhaps try (note additional escaped quotes around category:
'<a href="tel:' . $d['my_tel'] . '" onClick="_gaq.push([\'_trackEvent\', \'' . $d['my_category'] . '\', \'Event Action\',...
--dan
Some text before <img style="float: left;" src="../images/265218_imgthw.jpg" alt="" width="81" height="88"> some text after
Say i have something like that wrap inside some text coming from db. I need it to be changed to:
Some text before some text after
Any help please.
I tried breaking the code using the img tag with explode. It works fine but need to reduce the code using regEx. This is how i do it:
$string = "<img";
$explored = explode($string,$desc);
$desc = "";
foreach ($explored as $key => $value) {
static $counter = 0;
if(strpos($explored[$key], '/>')){
$desc .= "<a href='".$path.$image[$counter]."' class='lightview'><img".$explored[$key];
$counter++;
} else $desc .= $explored[$key]."<a href='".$path.$image[$key]."' class='lightview'><img";
}
$string = "/>";
$explored = explode($string,$desc);
$desc = "";
foreach ($explored as $key => $value) {
if(strpos($explored[$key], '<img'))
{
$desc .= $explored[$key]."/></a>";
}
else $desc .= $explored[$key];
}
You can do it using SimpleHTMLDOM pretend that you have a variable named as $t
// '$t' is the element to be wrapped with anchor
$tempHtml = str_get_html('<a>' . $t->outertext . '</a>');
$link = $tempHtml->find('a', 0);
// '$src' is the url of the link
$link->href = $image['src'];
I'm using the following PHP code to get the comments for a specific video:
<?php
$vid = "G0k3kHtyoqc";
$feedURL = 'http://gdata.youtube.com/feeds/api/videos/' . $vid;
$entry = simplexml_load_file($feedURL);
$gd = $entry->children('http://schemas.google.com/g/2005');
if($gd->comments->feedLink){
$attrs = $gd->comments->feedLink->attributes();
$commentsURL = $attrs['href'];
$commentsCount = $attrs['countHint'];
}
if($commentsURL && $commentsCount > 0){
$commentsFeed = simplexml_load_file($commentsURL);
echo "<ol>";
foreach($commentsFeed->entry as $comment){
echo "<li>";
echo "<a target='_blank' href='http://www.youtube.com/user/" . $comment->author->name . "'>";
echo $comment->author->name;
echo "</a>";
echo " - " . $comment->content;
echo "</li>";
}
echo "</ol>";
}
?>
The problem with the code above is that it only gets the most recent 24 comments. I need a way to paginate through all comments.
Your help is much appreciated.
Thanks
Use the "start-index" param. Begins on 1, and depending on the comments count, add [comments-count] to start-index param.
For example:
First page of comments, getting 25 comments per page, use max-results=25 and start-index=1
Second page of comments, getting 25 comments per page, use max-results=25 and start-index=26
And so on =)
Regards!
I have a fetch function that injects rss content into a page for me. This returns an xml which contains the usual RSS elements like title, link, description but the problem is the returned description is a table with two tds which one contains an image the other the text. I am not sure how I can remove the table, img and the tds and be left only with the text using php and not javascript.
Any help is much appreciated.
<?php
require_once('rss_fetch.inc');
$url = 'http://www.domain.com/rss.aspx?typeid=0&imagesize=120&topcount=20';
if ( $url ) {
$rss = fetch_rss( $url );
//echo "Channel: " . $rss->channel['title'] . "<p>";
echo "<ul>";
foreach ($rss->items as $item) {
$href = $item['link'];
$title = $item['title'];
$description = $item['description'];
$pubdate = date('F dS, Y', strtotime($item['pubdate']));
echo "<li><h3>$title<em>$pubdate</em></h3>$description <p><a href='$href' target='_blank'>ادامه مطلب</a></p><br/></li>";
}
echo "</ul>";
}
?>
strip_tags() will do the job..