I want to use PHP & QueryPath to find all images in a document, then modify its src like this:
I want to change
http://test.com/test/name.jpg
to
http://example.com/xxx/name.jpg
I can find the specific class name using
$qp2 = $qp->find('body');
Now when I want to find all img on it to change the src:
foreach ($qp2->find('img') as $i) {
//here change the src
}
But when I execute
echo $qp2->html();
I see only last image. Where is the problem?
Like this?
foreach($qp2->find('img') as $key as $img) {
echo $img->html();
}
Sometimes you have to use top() or end() when you are re-using the qp object. Something like:
$qp = htmlqp($lpurl);
foreach ($qp->find('img') as $key => $img){
print_r($img->attr('src'));
$url = parse_url ($img->attr('src'));
print_r($url);
echo '<br/>';
if (!isset($url['scheme']) && !isset($url['host']) && !empty($url['path'])){
$newimg = $htmlpath . '/' . $url['path'];
$img->end()->attr('src', $newimg);
echo $img->html();
}
}
foreach ($qp->top()->find('script') as $key => $script){
print_r($script->attr('src'));
$url = parse_url ($script->attr('src'));
print_r($url);
if (!isset($url['scheme']) && !isset($url['host']) && !empty($url['path'])){
$newjs = $htmlpath . '/' . $url['path'];
echo '<br/>';
echo 'this is the modified ' . $newjs;
}
}
Related
I am trying to get all links of contains specific url page on a given page using PHPQuery. I am using the PHP support syntax of PHPQuery.
include_once 'phpQuery.php';
$url = 'http://www.phonearena.com/phones/manufacturer/';
$doc = phpQuery::newDocumentFile($url);
$urls = $doc['a'];
foreach ($urls as $url) {
echo pq($url)->attr('href') . '<br>';
}
The code above works . But it shows all the links
I want to show only those containing "/phones/manufacturer/".
I tried this but it shows nothing:
include_once 'phpQuery.php';
$url = 'http://www.phonearena.com/phones/manufacturer/';
$doc = phpQuery::newDocumentFile($url);
$urls = $doc['a'];
foreach ($urls as $url) {
echo pq($url)->attr('href:contains("/phones/manufacturer/")') . '<br>';
}
Use below coding get all urls from that site,
$doc = new DOMDocument();
#$doc->loadHTML(file_get_contents('http://www.phonearena.com/phones/manufacturer/'));
$ahreftags = $doc->getElementsByTagName('a');
foreach ($ahreftags as $tag) {
echo "<br/>";
echo $tag->getAttribute('href');
echo "<br/>";
}
exit;
Try this, a little italian guide, jquery documentation
include_once 'phpQuery.php';
$url = 'http://www.phonearena.com/phones/manufacturer/';
$doc = phpQuery::newDocumentFile($url);
$urls = $doc['a[href*="/phones/manufacturer/"]'];
foreach ($urls as $url) {
echo pq($url)->attr('href') . '<br>';
}
I have the next code:
<?php
$path = 'imgsFor';
$files_array = scandir($path);
for ($x=0; $x<=4; $x++)
{
echo '<img src="imgsFor/$files_array[$x]" <br>';
}
?>
In order to display all images in the folder imgsFor.
For some reason, I see the just boxes and not the actual images.
What can be the reason?
The best way for me is to use glob function:
foreach (glob($path) as $filename) {
echo '<img src="' . $path . '/' . $filename . '"/><br/>';
}
You messed up some things. Your correct script would be
<?php
$path = 'imgsFor/';
$files_array = scandir($path);
foreach($files_array as $f) {
if(is_dir($path . $f) === false)
continue;
echo '<img src="' , $path , $f , '"><br>';
}
/* EOF */
The reason is that your URL is invalid. Your variable wont echo out if you use single quotes. You also forgot to end the tag. Try this:
echo "<img src='http://yourwebsite.com/imgsFor/{$files_array[$x]}'/><br/>";
Please check you directory path and use is_dir which returns false when the file doesn't exist. you can try like this
$path = 'imgsFor';
$scan = scandir($path);
foreach($scan as $file)
{
if (!is_dir($path))
{
echo $file.'\n';
}
}
I have this php code to check whether an image exists or not.
foreach($pic_switch as $pic_switch_key => $pic_switch_value)
{
if ($pic_switch_value == "no-image")
{
$img_url = 'http://www.reuters.com/resources_v2/images/masthead-logo.gif';
}
else
{
$img_url = $img_location . $pic_switch_key . '.jpg';
}
}
The above code works great.
I would like to echo $img_url outside the foreach. I tried:
echo '<pre>'.print_r($img_url,true).'</pre>';
but it only gives the URL of the last image. I would like to display the URL of all the images. I would like to display all of them outside the Foreach, rather than echo the URL inside it.
When you use foreach this way, you will end up storing the last variable only in the $img_url. So use this way:
foreach ($pic_switch as $pic_switch_key => $pic_switch_value)
{
if ($pic_switch_value == "no-image")
{
$img_url = 'http://www.reuters.com/resources_v2/images/masthead-logo.gif';
}
else
{
$img_url = $img_location . $pic_switch_key . '.jpg';
}
echo '<pre>' . print_r($img_url, true) . '</pre>';
}
I don't understand why you can't echo from within a loop but one method would be to build a string within the loop and echo it later in the script.
It would be neater to build an array of the URLs and echo from within a for loop later.
Using PHP Simple HTML DOM Parse but unable to get images to display.
I am not a coder and am trying to pull articles and images from a website. The articles are fine but the images are not displaying. Instead part of the path displays e.g.
> //ssl.gstatic.com/ui/v1/button/search-white.png
> //ssl.gstatic.com/ui/v1/button/search-white.png
> //ssl.gstatic.com/ui/v1/icons/common/settings.png
Using Google as an example, here's the code I am using:
<?php
$html = file_get_html('https://news.google.com/nwshp?hl=en&tab=in');
foreach($html->find('h2') as $e)
echo $e->innertext . '<br><br>';
foreach($html->find('div.jsdisplay') as $e)
echo $e->innertext . '<br>';
foreach($html->find('img') as $element)
echo $element->src . '<br>';
?>
Thanks for any help
You should replace
foreach($html->find('img') as $element)
echo $element->src . '<br>';
With
foreach ( $html->find('img') as $element ) {
$img = str_replace(array("//ssl"), array("http://ssl"), $element->src);
for($i = 0; $i < 5; $i ++) {
$img = str_replace("//nt$i", "http://nt$i",$img);
}
echo "<img src=\"$img\" /> <br>";
}
Update my answer after your last comment with your original site URL 'http://frielatvsales.com/QuadAttachments.htm'
try below code.
include_once "simplehtmldom/simple_html_dom.php";
$url = "http://frielatvsales.com/QuadAttachments.htm";
$html = file_get_html($url);
preg_match('#^(?:http://)?([^/]+)#i', $url, $matches);
$host = $matches[1];
foreach($html->find('h2') as $e) {
echo $e->innertext . '<br><br>';
}
foreach($html->find('div.jsdisplay') as $e) {
echo $e->innertext . '<br>';
}
foreach($html->find('img') as $element) {
echo '<img src=http://'.$host.'/'.$element->src . ' /><br>';
}
//ssl.gstatic.com/ui/v1/button/search-white.png is a relative URI (the scheme is not specified, so it will use the same scheme (e.g. http: or https:) as the page it appears in).
Resolve it as you would any other relative URI.
My question is how to get the images to display using the code in my original post.
You have to output an <img> tag instead of the URI as plain text.
The XML feed is located at: http://xml.betclick.com/odds_fr.xml
I need a php loop to echo the name of the match, the hour, and the bets options and the odds links.
The function will select and display ONLY the matchs of the day with streaming="1" and the bets type "Ftb_Mr3".
I'm new to xpath and simplexml.
Thanks in advance.
So far I have:
<?php
$xml_str = file_get_contents("http://xml.betclick.com/odds_fr.xml");
$xml = simplexml_load_string($xml_str);
// need xpath magic
$xml->xpath();
// display
?>
Xpath is pretty simple once you get the hang of it
you basically want to get every match tag with a certain attribute
//match[#streaming=1]
will work pefectly, it gets every match tag from underneath the parent tag with the attribute streaming equal to 1
And i just realised you also want matches with a bets type of "Ftb_Mr3"
//match[#streaming=1]/bets/bet[#code="Ftb_Mr3"]
This will return the bet node though, we want the match, which we know is the grandparent
//match[#streaming=1]/bets/bet[#code="Ftb_Mr3"]/../..
the two dots work like they do in file paths, and gets the match.
now to work this into your sample just change the final bit to
// need xpath magic
$nodes = $xml->xpath('//match[#streaming=1]/bets/bet[#code="Ftb_Mr3"]/../..');
foreach($nodes as $node) {
echo $node['name'].'<br/>';
}
to print all the match names.
I don't know how to work xpath really, but if you want to 'loop it', this should get you started:
<?php
$xml = simplexml_load_file("odds_fr.xml");
foreach ($xml->children() as $child)
{
foreach ($child->children() as $child2)
{
foreach ($child2->children() as $child3)
{
foreach($child3->attributes() as $a => $b)
{
echo $a,'="',$b,"\"</br>";
}
}
}
}
?>
That gets you to the 'match' tag which has the 'streaming' attribute. I don't really know what 'matches of the day' are, either, but...
It's basically right out of the w3c reference:
http://www.w3schools.com/PHP/php_ref_simplexml.asp
I am using this on a project. Scraping Beclic odds with:
<?php
$match_csv = fopen('matches.csv', 'w');
$bet_csv = fopen('bets.csv', 'w');
$xml = simplexml_load_file('http://xml.cdn.betclic.com/odds_en.xml');
$bookmaker = 'Betclick';
foreach ($xml as $sport) {
$sport_name = $sport->attributes()->name;
foreach ($sport as $event) {
$event_name = $event->attributes()->name;
foreach ($event as $match) {
$match_name = $match->attributes()->name;
$match_id = $match->attributes()->id;
$match_start_date_str = str_replace('T', ' ', $match->attributes()->start_date);
$match_start_date = strtotime($match_start_date_str);
if (!empty($match->attributes()->live_id)) {
$match_is_live = 1;
} else {
$match_is_live = 0;
}
if ($match->attributes()->streaming == 1) {
$match_is_running = 1;
} else {
$match_is_running = 0;
}
$match_row = $match_id . ',' . $bookmaker . ',' . $sport_name . ',' . $event_name . ',' . $match_name . ',' . $match_start_date . ',' . $match_is_live . ',' . $match_is_running;
fputcsv($match_csv, explode(',', $match_row));
foreach ($match as $bets) {
foreach ($bets as $bet) {
$bet_name = $bet->attributes()->name;
foreach ($bet as $choice) {
// team numbers are surrounded by %, we strip them
$choice_name = str_replace('%', '', $choice->attributes()->name);
// get the float value of odss
$odd = (float)$choice->attributes()->odd;
// concat the row to be put to csv file
$bet_row = $match_id . ',' . $bet_name . ',' . $choice_name . ',' . $odd;
fputcsv($bet_csv, explode(',', $bet_row));
}
}
}
}
}
}
fclose($match_csv);
fclose($bet_csv);
?>
Then loading the csv files into mysql. Running it once a minute, works great so far.