This is my code:
<?php
include("includes/simple_html_dom.php") ;
$url_to_get = "http://getconfused.net/" ;
$homePage = file_get_html($url_to_get);
$allLinks = $homePage->find('a');
foreach ( $allLinks as $link)
{
$href = $link->innertext ;
echo $href . "</br>" ;
}
?>
Simple. Just fetch a page, find any links and print the innertext(<a >innertext</a>) . But for some reason simple html dom here is skipping a lot of links. TO be specific, its missing all the links from the first div (<div id="getconfused">) of the page.
Why ? what can one do to remedy the problem?
Is that div in the body? And is the page otherwise valid?
Probably the html is corrupt in that part, causing the div to be skipped.
Related
Hi there I'm new here.
Trying to make this little code to loop over pages. And scrape off links of headings .
Scraping part works just fine but i cant make it to loop to the next page. It keeps looping on the same page.
<?php
include('../simple_html_dom.php');
// start at page 1
$xder = 1;
do {
// web page + page number (should change with every loop)
$html = file_get_html('https://webpage.com/stuff/page/$xder');
foreach($html->find('h3') as $h3)
{
foreach($h3->find('a') as $element)
{
echo $element->href . '<br>';
}
}
$xder++;
} while ($xder <= 5);
?>
I'm expecting to get list of links from all 5 pages, but I only get list of links from 1st page repeating 5 times.
I think the problem is here "/stuff/page/$xder');" I'm not sure how to add a variable to the back of an URL it doesn't appear to work.
Tried methods here:
Converting an integer to a string in PHP
Getting frustrated with this. Not sure what I'm missing here. Thanks for any thoughts :)
Php variables are treated as variables only if you use ", and not '
Change
$html = file_get_html('https://webpage.com/stuff/page/$xder');
to
$html = file_get_html("https://webpage.com/stuff/page/{$xder}");
I'm trying to print an xml feed into my php page,but this code is not working correctly, and i have no idea why. It just shows the code as it is on the browser from xpath to ?> . Can anyone help me with this please
<html>
<head>
<title>XML FEED</title>
</head>
<body>
<ul>
<?php
$dom = simplexml_load_file("http://feeds.bbci.co.uk/news/rss.xml");
foreach ($dom->xpath("/channel/item") as $item)
{
print "<li>";
print $item->title;
print "</li>";
}
?>
</ul>
</body>
</html>
Consider adjusting your XPath with double forward slashes as channel is not the root element:
foreach ($dom->xpath("//channel/item") as $item) {
...
}
Alternatively, use the root, rss, element in expression:
foreach ($dom->xpath("/rss/channel/item") as $item) {
...
}
I suspect your server is not configured to allow the short PHP tags (which is discouraged anyway) so Always use the full opening tag.
Since PHP is not parsing your code, the browse gets sent the following...
<? . . . . . foreach ($dom->
Which is not valid HTML, so it is not displayed, but if you view the source of your page, you will see more of your PHP code.
Simply starting your code with <?php will trigger PHP parsing, and things should work.
I am trying to parse a html file using PHP Simple HTML DOM Parser, I am facing a div with a class which contains a space character
<div class="camera_src camerastarted">
<div data-thumb="/images/articles/football/th/css_sg_th.jpg" data- src="/images/articles/football/css_sg.jpg">
I used the following code:
$link = $html->find('.camera_src camerastarted div');
print_r($link);
foreach ($link as $ligne)
{
echo '-- ' . $ligne->getAttribute('data-src') . '<br />';
}
But I got nothing as display, only: Array()
What's wrong? thanks for advance!
You can select only one class like this
$link = $html->find('.camerastarted');
Have you tried this:
$link = $html->find('.camera_src.camerastarted');
Notice there is an error in your HTML, a space in "data- src", not "data-src"
Of course you get nothing.
Here's the problem, I am trying to echo a statement or an array after dynamically generated HTML, and unfortunately the thing that i want to echo goes above the HTML, is there any way to echo it after that dynamic HTML or work around?
Code:
Link 1
Link 2
if(isset($_GET["id"]) && $_GET["id"] == "do_something") {
$html = "dynamic html generate";
echo $html;
//after this im using foreach
foreach($array as $item) { echo $item . "<br />"; }
}
As I click one of these two , dynamically generated HTML shows up. Now for example I have an array:
$array = array("error1", "error2");
All the generated PHP goes above the dynamic HTML :/.
How should i fix it so that i can echo all of this array below the dynamic HTML?
Thanks
Use buffering with ob_start
ob_start();
// dynamic html code generate
$dynamic_html = ob_get_clean();
echo $dynamic_html;
// your code
echo $dynamic_html;
Sounds like you missed some closing tags (most likely </table>) in the dynamic html. Thats why the later generated echo gets displayed at the top.
Example (Note the missing closing table):
<?php
echo "<table><tr><td>TableText</td></tr>";
echo "I should be bellow the table, but going to the top.";
?>
will produce:
I should be bellow the table, but going to the top.
TableText
I want to:
Read in text line from "textfile.txt".
'echo' that line to the page in a <div> element.
Read in a text line from "namefile.txt".
Make this line become some sort of pop-up-text for that <div> element.
My script:
<? PHP
$fhtext = fopen("textfile.txt","a+") or exit("Error 1");
$fhname = fopen("namefile.txt","a+") or exit("Error 2");
while(!feof($fhtext))
{
echo "<div title="HERE IS WHERE I AM STUCK">".fgets($fhtext)."<div/><br />";
}
Could I perhaps go:
echo "<div title="<? fgets($fhname) ?>".fgets($fhtext)."<div/><br />";
?
<?php
$fhtext = fopen("textfile.txt","a+") or exit("Error 1");
$fhname = fopen("namefile.txt","a+") or exit("Error 2");
while(!feof($fhtext) && !feof($fhname))
{
echo "<div title=\"", fgets($fhname), "\">", fgets($fhtext), "<div/><br />";
}
?>
I haven't used PHP in a long time, but this should work:
echo "<div title='" . fgets($fhname) ."'>" .fgets($fhtext). "<div/><br />";
Regarding:
Make this line become some sort of pop-up-text for that '' element.
If you mean 'popup' text, as in tooltips of the type you get when you hover over links/images, this is only available on some elements when their title attribute has been set, not DIVs.
As such you can either change the DIV to a A (link) element. Or use Javascript to detect a hover over the DIV and display a popup.
If you are sure both files have the same number of lines you could use the „file“-function of PHP. This will read the file into an array and you can loop over it with a for-loop:
<?php
$file1 = file('file1');
$file2 = file('file2');
for ($i = 0, $max = count($file1); $i < $max; $i++) {
echo $file1[$i].' '.$file2[$i];
}
Before you dump your fgets() data to the browser, you really ought to HTML encode it first. That will prevent accidental (or not so accidental) problems caused by HTML fragments that might be in your text files, or if the file name can be entered by the user (either as part of the URL or as part of a form).
As a rule of thumb, always HTML encode anything coming from a data source you don't control before spitting it out to the browser. That includes form fields, etc.