How to style the Simple_HTML_DOM output - php

I'm getting images and their url with the following code using Simple HTML DOM Parser:
<?php
include_once('simple_html_dom.php');
$url = "http://www.tokyobit.com";
$html = new simple_html_dom();
$html->load_file($url);
foreach($html->find('img') as $img){
echo $img . "<br/>";
echo $img->src . "<br/>";
}
?>
But the output doesn't look so nice:
(source: netdna-cdn.com)
So how can I style the outputs in CSS like with adding a class to each image and it's src.
My CSS:
.image-and-src {
border: 2px solid #777;
}
So how can I add that class? : image-and-src

foreach($html->find('img') as $img){
echo '<div class="img-and-src">';
echo $img . "<br/>";
echo $img->src . "<br/>";
echo '</div>';
}
The two lines added to the code wraps the echo'd content in a div with your class while it loops.
Now you have the possibility to also wrap the text in a span, styling them both seperately.
If you want to add the class to just the image without styling the text, you could try #Ajeet Manral's answer :)

try this
foreach($html->find('img') as $img){
echo $img->src . "<br/>";
echo '<img src="'.$img->src.'" width=100% height=100px><br/>';
}

How about trying to make your own template
a template file
<!DOCTYPE html>
<html>
<head>
<title>Your title</title>
</head>
<body>
<h1>Somebody's images</h1>
<?php foreach($html->find('img') as $img) { ?>
<!-- put some pretty looking html here -->
<?php } ?>
<body>
</html>
if you don't know about templates, then I suggest some research on the subject

Related

WordPress - How to use PHP inside HTML injecting function?

I have a function that injects HTML code, the function looks like this:
function page_content() {
echo <<<HTML
<body>
<?php ?> //this won't work
</body
<style>
</style>
HTML;
}
Is there any way to write PHP code inside this HTML injection function?
This function is used for replacing the default WP Dashboard, but writing just HTML and CSS is limiting me totally :( Thanks
edit:
I have resolve this by breaking the HTML into two parts like this:
function page_content() {
echo <<<HTML
<body>
<div class="test">
<p>Body</p>
<p>Body</p>
<p>Body</p>
HTML;
echo do_shortcode('[contact-form-7 id="86" title="My Form"]');
echo <<<HTML
<p>Body</p>
<p>Body</p>
<p>Body</p>
</div>
</body>
<style>
.test {
display: flex;
flex-direction: column;
}
</style>
HTML;
}
If there is any better or easier way of doing this please let me know. I'm only learning, so always happy to learn the best ways of solving things in PHP. Thanks guys!
You can write like this.
<?php
function page_content() {
$output = 'HTML
<body>';
$output .= write php code here;
$output .=' </body
<style>
</style>
HTML';
echo output;
}
?>
I wrote like this so that you can understand easily but if you get used to combining HTML with php, you can just write together using .' and '. lol
<?php
function page_content() {
$output =
'HTML
<body>' . $write_anything_with_php .' </body>
<style>
</style>
HTML';
echo output;
}
?>

How to get image URL to display in PHP

Can't get image URL to display in my code; this is my URL: https://production.cdmycdn.com/webpack/renderer/d7285ffbbd0ca6d1d2179f7d22ea1f67.svg
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
<!DOCTYPE html>
<html>
<head>
<title>PHP Exercise 1: Links and Variables</title>
</head>
<body>
<h1>PHP Exercise 1: Links and Variables</h1>
<p>Use PHP echo and variables to output the
following link information:</p>
<hr>
<?php
$linkName ='<h1> Codecademy <h1>';
$linkURL = 'codecademy';
$linkImage =
'https://production.cdmycdn.com/webpack/renderer/d7285ffbbd0ca6d1d2179f7d22ea1f67.svg';
$linkDescription = 'Learn to code interactively, for free.';
$my_name = "peter";
echo "<img>" . $linkImage . "</img>";
echo $linkName;
echo "<br>";
echo $linkURL;
echo "<br>";
echo $linkImage;
echo "<br>";
echo $linkDescription;
$linkImage = 'https://production.cdmycdn.com/webpack/renderer/d7285ffbbd0ca6d1d2179f7d22ea1f67.svg';
echo '<img src="data:image/jpeg;base64,">';
?>
</body>
</html>
A link to an image should always be put in the src attribute when you're using the img tag in html.
That means you should be looking for something like "<img src=".$linkImage."></img>" instead of "<img>".$linkImage."</img>"
Of course, that only applies if the problem you're referring to is the fact that there should be an image after the sentence "Learn to code interactively, for free."
Works just fine for me. Try checking php permissions.

How to get AJAX RESPONSE text from DIV using PHP DOMDocument?

I have to recover some text from a div of a site. The div is structured as follows:
The HTML Markup:
<div class="content" id="content">
Loading.....
</div>
Content of DIV changes by AJAX function which is on onload of page I guess. and the content of DIV get changes after 1 or 2 seconds.and the HTML structure becomes:
<div class="content" id="content">
<span class"parent">
<span class="child">
<span class="sometext">HERE IS SOME TEXT</span>
</span>
</span>
</div>
When I use the following PHP function(crawl_page) to grab the HTML of div with ID content it always return (Loading..) which it should be.
What I need is the updated html code, is there anyway to achieve this ?
function crawl_page($url)
{
$doc = new DOMDocument();
$doc->loadHTMLFile($url);
$xpath = new DOMXPath($doc);
$element = $xpath->query("//*[#id='content']")->item(0);
echo $element->nodeValue;
}
crawl_page("http://example.com/#1:7");
i hope its working. And download include file from the below url
http://sourceforge.net/projects/simplehtmldom/files/
<?php
// example of how to use basic selector to retrieve HTML contents
include('../simple_html_dom.php');
// get DOM from URL or file
$html = file_get_html('http://example.com/#1:7');
// find all link
foreach($html->find('a') as $e)
echo $e->href . '<br>';
// find all image
foreach($html->find('img') as $e)
echo $e->src . '<br>';
// find all image with full tag
foreach($html->find('img') as $e)
echo $e->outertext . '<br>';
// find all div tags with id=gbar
foreach($html->find('div#content') as $e)
echo $e->innertext . '<br>';
// find all span tags with class=gb1
foreach($html->find('span.gb1') as $e)
echo $e->outertext . '<br>';
// find all td tags with attribite align=center
foreach($html->find('td[align=center]') as $e)
echo $e->innertext . '<br>';
// extract text from table
echo $html->find('td[align="center"]', 1)->plaintext.'<br><hr>';
// extract text from HTML
echo $html->plaintext;
?>

Search in html using PHP

I would like to find elements or to get image links in HTML, see example HTML below, i tried the PHP method to get the image link, but i dont know what is wrong with my code can someone help me please with example, thanks and thanks to Stackoverflow
example html:
<div class="items">
<div class='photoBorder'>
<a class="box-thumb" data-fancybox-group="thumb" href="//example.com/Resize134_700_1000.jpg"><img title="test" width="220" height="165" style="height:165px; width: 220px;" onerror="$(this).parent().parent().remove();" itemprop="image" src="//example.com/Resize134_700_1000.jpg" alt="test" />
</a>
</div>
</div>
My code :
foreach($html->find('div.items div.photoBorder a.box-thumb') as $imageLink)
{
$images[] = $imageLink->href;
}
If you want to parse HTML code using PHP you can use this library
its sample code looks like this
// Create DOM from URL or file
$html = file_get_html('http://www.google.com/');
// Find all images
foreach($html->find('img') as $element)
echo $element->src . '<br>';
// Find all links
foreach($html->find('a') as $element)
echo $element->href . '<br>';
And if your are fetching data from other sites or file this may be a good idea. I think you don't want to use this method to do manipulation in your front-end html in that case use jquery

display a captcha image in my page

i have to get the captcha image from a web page. for that i use phpquery and dom file like the following..
<?php
include 'phpQuery-onefile.php';
$html = file_get_contents("http://who.godaddy.com/whoisverify.aspx?domain=nettantra.com&prog_id=godaddy");
$pq = phpQuery::newDocument($html);
print $pq->find('img#whoisverify_ctl00_cphcontent_ctlcaptcha_CaptchaImage')->attr('src').'<br/>';
?>
<img src="<?php print $pq->find('img#whoisverify_ctl00_cphcontent_ctlcaptcha_CaptchaImage')->attr('src'); ?>" alt="captcha_image" />
<?php
echo '<br />';
require_once('../simple_html_dom.php');
$html = file_get_html('http://who.godaddy.com/whoisverify.aspx?domain=nettantra.com&prog_id=godaddy');
foreach($html->find('img') as $element) {
echo $element.'<br/>';
// echo $element->src, "\n";
}
?>
now, i have only the problem that it fetch the source, but cant get the image. is that impossible to save the captcha image in my page ?
Change img sourse like this
<img src="http://who.godaddy.com/<?php print $pq->find('img#whoisverify_ctl00_cphcontent_ctlcaptcha_CaptchaImage')->attr('src'); ?>" alt="captcha_image" />

Categories