I am building a mobile version of my company website, and one thing we are in need of is an RSS feed.
I have the RSS pulling in fine with this code:
<?php
$url = 'http://www.someurl.com/rss/articles';
$feed = simplexml_load_file($url, 'SimpleXMLIterator');
$filtered = new LimitIterator($feed->channel->item, 0, 15);
foreach ($filtered as $item) { ?>
<li data-icon="false">
<h2><?php echo $item->title; ?></h2>
<p class="desc"><?php echo $item->description; ?></p>
<br />
<p class="category"><b><?php echo $item->category; ?></b></p>
<a class="link" href="<?php echo $item->link; ?>">Read More</a>
<br />
<p class="pubDate"><?php echo $item->pubDate; ?></p>
<br />
</li>
<?php } ?>
What I would like to do is utilize either the fopen() or file_get_contents() to handle the clicking of the 'Read More' link and strip all of the contents of the incoming page except for the <article> tag.
I have searched Google the past day, and have not been successful in finding any tutorials on this subject.
EDIT:
I would like to load the stripped HTML contents into their own view within my framework.
SECOND EDIT:
I would just like to share how I solved this problem.
I modified my $item->link; to be passed through the URL as a variable:
Read More
On the article.php page, I collect the variable with a if() statement:
if (isset($_GET['rss_url']) && is_string($_GET['rss_url'])) {
$url = $_GET['rss_url'];
}
Then building on the suggestions of the comments below, I built a way to then collect the incoming URL and strip the necessary tags to then format for my mobile view:
<div id="article">
<?php
$link = file_get_contents($url);
$article = strip_tags($link, '<title><div><article><aside><footer><ul><li><img><h1><h2><span><p><a><blockquote><script>');
echo $article;
?>
</div>
Hopefully this helps anyone else who may encounter this problem :)
I'm not sure if I understand it correctly but are you trying to output the contents on the current page whenever someone clicks the more link?
I would probably use Javascipt to do that, maybe jQuery's .load() function which loads html from another page and allows you to load only specific fragments of a page.. but if you need to use php I would look into Simple HTML DOM Parser
$html = file_get_html($yourUrl);
$article = $html->find('article', 0); // Assuming you only have 1 article/page
echo $article;
The only way I can see is to set up your own separate script to route the links through.
So, instead of echo $item->link use
echo 'LinkProcessor.php?link='.$item->link
Then, setup a script called LinkProcessor.php and use file_get_contents on that page. You can then process the XML to only show the article tag and echo the results:
$article = file_get_contents($_GET['link']);
$xml = new SimpleXMLElement($article);
$articleXml = $xml->xpath('//article');
echo articleXml[0];
Note that the code is untested, but it should be OK.
Related
Im using lightgallery so i need to load images before call them in lightgallery. Problem is that images are large so it takes too much time to load. Is there any way to load that specific gallery when user click on link.
<div id="lightgallery-<?php echo get_the_ID(); ?>" class="hidelightgallery">
<?php
foreach ($files as $image) {
$image_attributes = wp_get_attachment_url( $image->ID );
$attachment_title = get_the_title($image->ID);
$caption = get_post_field('post_excerpt', $image->ID);
?>
<a class="item" href="<?php echo $image_attributes ?>" data-sub-html="<?php echo $attachment_title; ?> <?php if($caption!= '') echo ' - ' ?> <?php echo $caption ?>"><img src="<?php echo $image_attributes ?>"></a>
<?php } ?>
</div>
Now what i want is if user click for example link with this id then do foreach. Is that possible?
Just follow these steps,
create new template / html page where you will write html and populate by foreach loop
add your id = lightgallery whole code to that html page
when you will click on your link (which you mentioned) fire an ajax
Ajax function will get some id or number of images need to show or your logic on how you will populate data in foreach loop
in php you will get all relevant data, and you will populate that data in html file you created in step 1
php function will return that data to ajax function
Ajax function will get all your dynamic html data
populate that html where ever you want or just append that html wherever you want
Go step by step, this will solve your problem.
I have this website were you can order products.
The title of the products you can order are in HTML:
<div class="whatever"> Title </div>
I want to retrieve this "title" and set my php variable $product to the value "Title".
I have search a lot on the internet but somehow I am not able to find my answer.
How can I do it?
You can use \DOMDocument->loadHTML();
Ex:
<?php
$doc = new \DomDocument();
$doc->loadHTML('<div class="whatever"> Title </div>');
View examples here:
http://docs.php.net/manual/en/domdocument.loadhtml.php
This is assuming that your source is available to php. It would probably be more pragmatic to extract the value with javascript in the client and send it with the page request. If your app is well structured, the logic that renders the title into the page in the first place is probably where you should be looking to retrieve the information rather than trying to parse the html separately.
If you mean that you would like to do this from the client side, you should be using AJAX to achieve this. However, I think you mean that you want to put the HTML in a variable. That is very simple:
$variable = "<div class=\"whatever\"> Title </div>";
And to output the HTML:
echo $variable;
You can also add multiple elements to a single variable by concatting.
$variable = "";
$variable .= "<div class=\"whatever\"> Title </div>";
$variable .= "<div class=\"whatever\"> Another Title </div>";
echo $variable;
If you mean that you want to echo a variable within a dv, that works exactly the same way:
<div class="title"><?php echo $product; ?></div>
Or better looking:
<div class="title"><?= $product; ?></div>
I have same php to generate HTML in 2 ocasions:
Generate a link with target="_new";
Generate a link without target property.
The only way that I have to differentiate both of them is to create the parent div as different ID (eg: <div id="new"> for the 1st, '' for the 2nd.
Is there any way to check if has some #new in html and them set target?
Here's the way that I've tried so far:
<?php $new = $html->find("#new"); ?>
<a href="<?php echo $item->getPrimaryLink()->getUrl(); ?>" <?php if (is_object($new)): ?> target="_new" <?php else : ?> <?php endif; ?> >
If you are following HTMLDOMPARSER then you can follow:
$html = file_get_html('http://www.google.com/');
$is_new_exist=false;
if($html->find('div#new'))
$is_new_exist=true;
And now you can use that flag for your checking
For further query please checkout HTMLDOMPARSER
I would presume you would be able to use something like
$new = $html->find("#new");
if ($new) {
echo something
} else {
echo something else
}
Based on the assumption you are using domdocument or a html parser.
You would not use target however but rather do something like <a href="#new" or if it were on another page <a href="somepage.php#new"
In PHP i'm using the following code to put a banner at the top of my website.
$eventinfo = simplexml_load_file("eventinfo.xml");
<div id="eventinfo"><?php foreach($eventinfo->children() as $child){ $final = $child["name"]."...<a href='".$child["adr"]."'>more info...</a>"; } ?>
</div>
The XML doc is available at the following: http://eastsidespeedway.raceresults.co/eventinfo.xml
If you go to http://eastsidespeedway.raceresults.co/index.php you'll see that the more info... link is showing up twice. One with the correct link, and the other with a link to the same page (index.php).
Can anyone shed some light on what I'm doing wrong?
Also. If you see anything that I'm doing wrong, or you know something that's easier - let me know! This is my first time using XML/PHP so I'm kinda just wingin it. Haha.
this will work for you
<?php
$doc = new DOMDocument();
$doc->load('http://eastsidespeedway.raceresults.co/eventinfo.xml');
$title = $doc->getElementsByTagName('title');
$link = $doc->getElementsByTagName('link');
//print_r($eventinfo);
?>
<div id="eventinfo">
<?php echo $title->item(0)->getAttribute('name'); ?>
<a href='<?php echo $link->item(0)->getAttribute('adr'); ?>'>More Infoo..</a>
</div>
If you look at your source:
<div id="eventinfo">5/18/2013 - Al Smiley Memorial...<a href=''>more
info...</a>...<a href='http://www.eastsidespeedway.com/dirt.html'>more
info...</a></div>
You've got two hyperlinks- one href is blank meaning that it will redirect to the current page, check your HTML code first to see if you've accidentally duplicated the element, otherwise look at the construction of your string in the php code
What I'm trying to do is to add some HTML tags to my Joomla! module titles. I will need something like this
Some <b>Title</b>
but when I save !Joomla trims the titles and remove all HTML tags.
I've check the administrator/com_content, which I think should be responsible for inserting the database data, but I couldn't find the answer there.
Can anyone help me with this headache?
Check out ../templates/system/html/modules.php
You can style your module structure in HTML.
function modChrome_myCustomModule($module, &$params, &$attribs)
{
$doc =& JFactory::getDocument();
$css = ".otherClass {}";
$css .= ".yourClass {}";
$doc->addStyleDeclaration($css);
?>
<div>
<?php if ($module->showtitle != 0) : ?>
<h1><?php echo $module->title; ?></h1>
<?php endif; ?> // post your title
</div>
<div>
<?php echo $module->content; ?> // post your module content
</div>
<?php
}
Then call your styled module in index.php:
<jdoc:include type="modules" name="right" style="myCustomModule" />
So I found the solutions. It includes both of the previous answers, so I'm putting a new one here with the correct code.
First of all I need to say, that this solution works only for a fixed amount of words (last one, two, etc.) I need only to have the last one, so I will post an example code with one word.
First as SMacFadyen sad I needed to create a new module structure in my template html folder: /templates/system/html/modules.php file.
Note: If you don't want to add this new module styling to all templates, but just on one of them you need to put the module.php in your template's html folder.
The provided by SMacFadyen looks like this:
function modChrome_myCustomModule($module, &$params, &$attribs)
{
$doc =& JFactory::getDocument();
$css = ".otherClass {}";
$css .= ".yourClass {}";
$doc->addStyleDeclaration($css);
?>
<div>
<?php if ($module->showtitle != 0) : ?>
<h1><?php echo $module->title; ?></h1>
<?php endif; ?> // post your title
</div>
<div>
<?php echo $module->content; ?> // post your module content
</div>
<?php
}
Then expired by the comments of Hanny I've added some php code to match the last word of the title and to store it in a new varibale.The code looks like this:
$wrap_tag = 'b';
$html_title = preg_replace("~\W\w+\s*$~", '<'.$wrap_tag.'>'.'\\0'.'</'.$wrap_tag.'>', $module->title);
Note: the $wrap_tag variable stores the tag you want. You can put b, em, u and etc. to have different result.
The last thing was to replace the displayed title, so I've replaced this code:
<h1><?php echo $module->title; ?></h1>
with this one:
<h1><?php echo $html_title; ?></h1>
The final result was this:
function modChrome_myCustomModule($module, &$params, &$attribs)
{
$doc =& JFactory::getDocument();
$css = ".otherClass {}";
$css .= ".yourClass {}";
$wrap_tag = 'b';
$html_title = preg_replace("~\W\w+\s*$~", '<'.$wrap_tag.'>'.'\\0'.'</'.$wrap_tag.'>', $module->title);
$doc->addStyleDeclaration($css);
?>
<div>
<?php if ($module->showtitle != 0) : ?>
<h1><?php echo $html_title; ?></h1>
<?php endif; ?> // post your title
</div>
<div>
<?php echo $module->content; ?> // post your module content
</div>
<?php
}
Thanks to everybody for the help.
The Gantry framework can help you accomplish what you want (1st word styled one way, 2nd word styled another) - but it's a lot of overhead just accomplish that one task you're looking for. Ultimately you'll have to create a template override for your template, and then do some creative editing with php in order to get it to display that way.
There's no quick and easy way to get that done. You'll have to do some php coding on the backend and edit the template (use an override so you don't hack core files). Ultimately you'll probably have to code the php to pull apart the title, and apply formatting to each pulled apart word (or string of words as the case may be) using CSS.
Hope that helps.