Parse img from RSS-feed using PHP SIMPLE HTML DOM Parser - html

I am trying to parse this site (to get the img-link): http://statigr.am/feed/parishilton
This is my code:
include 'parse/simple_html_dom.php';
// Create DOM from URL or file
$html = file_get_html('http://statigr.am/feed/parishilton/');
// Find all images
foreach($html->find('img') as $element)
{
echo $element->src . '<br>';
}
The script doesn't return anything! Why is that ? I want the img link.

It's because all images are inside CDATA section and parser ignores it, so the solution is
$html = file_get_html('http://statigr.am/feed/parishilton/');
$html = str_replace("<![CDATA[","",$html); // clean-up
$html = str_replace("]]>","",$html); // clean-up
$html = str_get_html($html); // re-construct the dom object
// Loop
foreach($html->find('item description img') as $el)
{
echo $el->src . "<br />";
}
Replace all CDATA from the returned content and then use str_get_html to create DOM object from that string and loop through the images. (Tested and works).
Output :
http://distilleryimage3.s3.amazonaws.com/cc25d8562c9611e3a8b922000a1f8ac2_8.jpg
http://distilleryimage7.s3.amazonaws.com/4d8e22da2c8911e3a6a022000ae81e78_8.jpg
http://distilleryimage5.s3.amazonaws.com/ce6aa38a2be711e391ae22000ae9112d_8.jpg
http://distilleryimage3.s3.amazonaws.com/d64ab4c42bc811e39cbd22000a1fafdb_8.jpg
......
......

Related

How to include HTML tags when making a React\Http\Message\Response (ReactPHP)

How can I write tags from HTML in React\Http\Message\Response?
<?php
use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Loop;
use React\Http\HttpServer;
use React\Http\Message\Response;
use React\Socket\SocketServer;
require_once __DIR__ . '/vendor/autoload.php';
$loop = Loop::get();
$server = new HttpServer(function (ServerRequestInterface $request) {
return new Response(
200, ['Content-Type' => 'application/json'], "<h1>Hello World</h1>"
);
});
$socket = new SocketServer('127.0.0.1:8000');
$server->listen($socket);
echo 'Listening on ' . str_replace('tcp', 'http', $socket->getAddress()) . PHP_EOL;
$loop->run();
My code writes out "<h1>Hello World</h1>;" instead of making an h1 element with the contents of "Hello World". How can I make it return an h1 element instead of just writing it out with tags
ReactPHP core developer here. Through the Content-Type header you're telling the browser the content is JSON with the current value of application/json. When you set the Content-Type header to text/html the browser will render it as HTML.

Add default code in wordpress post

I want to add a simple code to all new post by default
I tried to used this code in .function.php (wpbeginner)
add_filter( 'default_content', 'my_editor_content' );
function my_editor_content( $content ) {
$content = "If you like this post, then please consider retweeting it or sharing it on Facebook.";
return $content;
}
I am using plugin named Shortcodes Ultimate
Code:
[su_spoiler title="Download The File" style="fancy" icon="chevron-circle"]Here[/su_spoiler]
I tried to normal use simple html and CSS code but it show the same error for both cases
https://i.stack.imgur.com/wgAs4.png
Try this -: wrap your code is a single quote as you are already using double quotes.
add_filter( 'default_content', 'my_editor_content' );
function my_editor_content( $content ) {
$content = '[su_spoiler title="Download The File" style="fancy" icon="chevron-circle"]Here[/su_spoiler]';
return $content;
}

Convert DOMXpath to an HTML string

Is there way to convert the DOMXpath object back to HTML? I would like to replace one section of the HTML
<div class='original'>Stuff</div>
replaced with:
<div class='replacement'>New Stuff</div>
and then return it back to a valid Xpath. I know that the function DOMDocument::saveHTML exists, but if I do
XPATH->saveHTML();
I get an error. Any advice would be appreciated.
Looks like an XY problem. DOMXPath always works on a DOMDocument instance, so you should always save the DOMDocument instead. See a working demo example below :
<?php
$xml = "<parent><div class='original'>Stuff</div></parent>";
$doc = new DOMDocument();
$doc->loadXML($xml);
$xpath = new DOMXpath($doc);
//get element to be replaced
$old = $xpath->query("/parent/div")->item(0);
//create new element for replacement
$new = $doc->createElement("div");
$new->setAttribute("class", "replacement");
$new->nodeValue = "New Stuff";
//replace old element with the new one
$old->parentNode->replaceChild($new, $old);
//TODO: save the modified HTML instead of echo
echo $doc->saveHTML();
?>
eval.in demo
output :
<parent><div class="replacement">New Stuff</div></parent>

WordPress Shortcode adding attributes

I'm writing a shortcode function for WP. Inside the function I have the $content (in this case an image). I need to be able to add a couple attributes to that content and return it in a string, like so:
function Shortcode( $atts, $content=null ){
if(!is_null($content){
return '<div class="wrapper">'. $content .'</div>'
}
}
Currently, just the image is getting spit back, but I need to somehow modify it and add a couple attributes before adding it to the string above.
I need to do something like this:
function Shortcode( $atts, $content=null ){
if(!is_null($content){
// CONVERT $CONTENT TO STRING
$content_html=somefunction($content)
echo $content_html; // <img src='....' />
return '<div class="wrapper">'. $content_html .'</div>'
}
}

Get text from XML file and print it in HTML File

I have a simple XML file created in R that consists of the following lines:
<statistics>
<mean>15.75</mean>
<sd>2.83</sd>
</statistics>
I want to extract the mean and sd to a HTML page, that has a Flash graph and I would like this underneath:
Statistics
Mean = 15.75
Standard Deviation = 2.83
What is the easiest way to achieve this?
Regards,
Anthony.
You should use PHP and SimpleXml.
Just load your xml with simplexml:
$xml = new SimpleXMLElement(file_get_contents("statistics.xml"));
and afterwards echo the desired elements to the page (or add them to your template engine):
echo "Mean: ".$xml->statistics[0]->mean."<br />";
echo "Standard Deviation: ".$xml->statistics[0]->sd."<br />";
If you have more then one statistics element, for example:
<statistics>
<mean>15.75</mean>
<sd>2.83</sd>
</statistics>
<statistics>
<mean>25.75</mean>
<sd>28.3</sd>
</statistics>
Simply use a foreach loop to iterate trough each element:
foreach ($xml->statistiscs as $statistic) {
echo "Mean: ".$statistics->mean."<br />";
echo "Standard Deviation: ".$statistics->sd."<br />";
}
jQuery is one way to do it. This will dynamically load the xml file on the client. The downside is that you have to include the library which you can download here: www.jquery.com. It can be used to so many other things so look into it. Your code would be something like:
$(document).ready(function () {
$.get('address/to/xml.file', function(data) {
var mean = $(data).find('mean').text();
var sd = $(data).find('sd').text();
$("somecontainer").text(mean + "|" + sd);
});
});
Or do you want some server-side language to do your xml parsing and printing?