I am working through using simple_html_dom.php to scrape and edit/manipulate the following:
<?php
include('simple_html_dom.php');
$_GET["name"];
$html_code="https://hwb.wales.gov.uk/Home/Pages/Home.aspx";
$html_code= $html_code.$name."/?lang=en";
echo $html_code;
$html = file_get_html($html_code);
echo "<html>";
echo "<head>";
echo "<meta charset='UTF-8'>";
echo "<title>PHP Test</title>";
echo " </head>";
echo " <body>";
foreach($html->find('#LatestNewsArts') as $e)
// Code here to append hwb.wale.gov.uk to <img src="/ >
echo $e->innertext . '<br>';
echo " </body>";
echo "</html>";
?>
I can extract the <div> that I'm looking for - and echo it -- that works fine.
Where I hit a wall (my .php-fu is letting me down) is how to I intercept and edit the html inside the e$ that I have scraped?
What I am looking to do, is replace the <img src="/...."> tag with <img src="hwb.wales.gov.uk/....">
Setting a new value to an attribute can easily be done like this: $elmt->attribute = NewValue
Here's a working code answering your question:
// includes Simple HTML DOM Parser
include "simple_html_dom.php";
$html_code="https://hwb.wales.gov.uk/Home/Pages/Home.aspx";
// => I dont know what $name stands fore... It's up to you to change this code to suit your needs
//$html_code= $html_code.$name."/?lang=en";
echo $html_code;
$html = file_get_html($html_code);
echo "<html>";
echo "<head>";
echo "<meta charset='UTF-8'>";
echo "<title>PHP Test</title>";
echo " </head>";
echo " <body>";
// Loop through all divs with id="Article"
foreach($html->find('#LatestNewsArts #Article') as $e){
$url = "https://hwb.wales.gov.uk" . $e->find("img",0)->src;
// Set src to the new $url
$e->find("img",0)->src = $url;
// Print the outertext
echo $e->outertext . '<br>';
}
echo " </body>";
echo "</html>";
// Clear dom object
$html->clear();
unset($html);
=> Working Demo <=
Related
I succeed at converting a XML file into a HTML one, but the output file is horrible. I would like a HTML file with the structure of the XML file (including clickable links).
My code :
public function SitemapHTML() {
$dom = new DomDocument();
$dom->formatOutput = TRUE;
$dom->load('sitemap.xml');
$data = $dom->getElementsByTagName('loc');
echo '<!DOCTYPE html>';
echo '<HTML>';
echo '<HEAD>';
echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
echo '</HEAD>';
echo '<BODY>';
echo ("<table>");
foreach ($data as $node)
{
echo '<tr>';
$thisurl = $node->textContent;
echo ("<td>" .
'' . $thisurl . '' .
"</td>");
echo '</tr>';
}
echo ("<table>");
echo '</BODY>';
echo '</HTML>';
$dom->saveHTMLFile('ok.html');
}
HTML output file :
I have looked all over google and stackoverflow. Tried many things, but nothing.
I would like a HTML file that has the structure of a common XML sitemap file.
I am making a self processing php form. I have a header.inc and footer.inc both called with a require statement. My problem lies in my header uses a $title variable which I would like to update after post. Currently my variable is changing and I can echo it in my response (thank you) page, but the header does not reload to reflect the change.
I understand I could call the title in my main PHP file, but I was hoping to find a way to reload the require(header.inc) file and continue processing the form.
<?php
$title = "XX";
$title2 = "Form";
$thisScript = htmlspecialchars($_SERVER['PHP_SELF']);
require("htmlHead.inc");
function changeTitle()
{
$GLOBALS['title2'] = "Results";
}
changeTitle();
if (!isset($_POST['submit']))
{
echo <<<FORMDOC1
<form action="$thisScript" method="post" action=" "
onSubmit="window.location.reload()">
FORMDOC1;
else
{
//random code
}
require("htmlFoot.inc");
?>
header.inc file in question:
<?php
echo "<!doctype html> <!-- Author: $author -->\n";
echo "<html lang=\"en\"><!-- Date Written: $dateWritten -->\n";
echo "<head> <!-- Description: $description -->\n";
echo "\t<title>$title</title>\n";
echo "\t<title>$title2</title>\n";
echo "\t<meta charset=\"utf-8\" />\n";
echo "\t<link rel=\"stylesheet\" href=\"css.css\" />\n";
echo "</head>\n";
echo "<body>\n";
echo "<div id=\"wrapper\">\n";
echo "<h1>$title</h1>\n";
echo "<h2>$title2</h2>\n";
?>
You are setting the title after including the header. Therefore, the value hasn't changed yet at the time php has output the header content.
Try following this general structure
if(isset($_POST['submit')) {
//process the form
include('header.inc');
//display confirmation
}
else {
include('header.inc');
//display the form
}
include('footer.inc');
Please Help, nothing i add after the ?> works, i tried to put the code in a echo, but its not working would someone please put together a JSFiddle for me or point me in the right direction, i am fairly new with PHP, Thanks for the help
<?php
$filepath = 'http://www.godsgypsychristianchurch.net/music.json';
$content = file_get_contents($filepath);
$json = json_decode($content, true);
foreach ($json['rows'] as $row)
{
if ($_GET['album'] == $row[doc]['album'])
{
echo "<title>{$row[doc]['album']}</title>";
echo "<table align=\"center\" border=\"0\"><tr><td valign=\"top\" width=\"330\">";
echo "<img src=\"{$row['doc']['artwork']}\" alt=\"my image \" width=\"250\" /><br /><br />";
echo "<div class=\"albuminfo\" id=\"albuminfo\">";
print ' Download entire album.<p>';
echo "<font color=\"#fff\">Album: {$row[doc]['album']}</font><br />";
echo "<font color=\"#fff\">Church: {$row[doc]['church']}</font><br />";
echo "<font color=\"#fff\">Description: {$row[doc]['des']}</font><P><br /><P>";
echo "Tweet<br><br>";
print '<div id="like-button"></div>';
echo "<td valign=\"top\">";
echo "<div class=\"playlist\" id=\"playlist\">";
echo "<ol>";
$songCount = 0;
foreach ($row['doc']['tracks'] as $song) {
++$songCount;
$songUrl = $row['doc']['baseurl'] . urldecode($song['url']);
echo "<li>{$song['name']}<div id=\"download\">Download</li>";
}
echo "</ol>";
echo "<br><div id=\"player\"><audio preload></audio></div>";
echo "</div>";
echo "<P>";
echo "<small>To download a single MP3 at a time:</br><b>Windows OS:</b> hold the ALT button on the keyboard and click the Download button<br><b>Mac OSX:</b> hold the OPTION button on the keyboard and click the Download button<P><BR><b>Controls:</b><br>Play/Pause = spacebar</br>Next track = Right arrow<br>Previous track = Left arrow";
echo '</tr></td></table>';
}
}
exit;
?>
<!----NOTTING SHOWING UP---->
<!-- begin htmlcommentbox.com -->
<div id="HCB_comment_box">HTML Comment Box is loading comments...</div>
<script type="text/javascript" language="javascript" id="hcb"> /*<!--*/ if(!window.hcb_user){hcb_user={};} (function(){s=document.createElement("script");s.setAttribute("type","text/javascript");s.setAttribute("src", "http://www.htmlcommentbox.com/jread?page="+escape((window.hcb_user && hcb_user.PAGE)||(""+window.location)).replace("+","%2B")+"&opts=0&num=10");if (typeof s!="undefined") document.getElementsByTagName("head")[0].appendChild(s);})(); /*-->*/ </script>
<!-- end htmlcommentbox.com -->
Because you are using exit, which terminates the script. Get rid of that and it will continue to output the HTML underneath.
http://php.net/manual/en/function.exit.php
Omit the exit.
Exit will end Php processing.
With php exit the parser stops, and hands back all the content gathered until that point to the web server. Simply delete the exit; row.
For example if I had the script:
<?php
$page = "My Page";
echo "<title>" . $page . "</title>";
require_once('header.php');
require_once('content.php');
require_once('footer.php');
?>
Is there something I can add to the bottom of that page to show the entire pre-compiled php?
I want to literally echo the php code, and not compile it.
So in my browser I would see the following in code form...
// stuff from main php
$page = "My Page";
echo "<title>" . $page . "</title>";
// stuff from require_once('header.php');
$hello = "Welcome to my site!";
$name = "Bob";
echo "<div>" . $hello . " " . $name . "</div>";
// stuff from require_once('content.php');
echo "<div>Some kool content!!!!!</div>";
// stuff from require_once('footer.php');
$footerbox = "<div>Footer</div>";
echo $footerbox;
Is this possible?
There's no way to do it native to PHP, but you could try to hack it if you just wanted something extremely simplistic and non-robust:
<?php
$php = file_get_contents($_GET['file']);
$php = preg_replace_callback('#^\s*(?:require|include)(?:_once)?\((["\'])(?P<file>[^\\1]+)\\1\);\s*$#m', function($matches) {
$contents = file_get_contents($matches['file']);
return preg_replace('#<\?php(.+?)(?:\?>)?#s', '\\1', $contents);
}, $php);
echo '<pre>', htmlentities($php), '</pre>';
Notes:
Warning: Allowing arbitrary file parsing like I've done with the fist line is a security hole. Do your own authentication, path restricting, etc.
This is not recursive (though it wouldn't take much more work to make it so), so it won't handle included files within other included files and so on.
The regex matching is not robust, and very simplistic.
The included files are assumed to be statically named, within strings. Things like include($foo); or include(__DIR__ . '/foo.php'); will not work.
Disclaimer: Essentially, to do this right, you need to actually parse the PHP code. I only offer the above because it was an interesting problem and I was bored.
echo '$page = "My Page";';
echo 'echo "<title>" . $page . "</title>";';
echo file_get_contents('header.php');
echo file_get_contents('content.php');
echo file_get_contents('footer.php');
For clarity I'd put the title generation in it's own file, then just use a series of echo file_get_contents()...
echo file_get_contents('title.php');
echo file_get_contents('header.php');
echo file_get_contents('content.php');
echo file_get_contents('footer.php');
I want to know how to get the original URL from php
For example:
example.php
<?php
header('location:test.php');
?>
I want to get test.php from example.php.
example.php
<?php
header('location:' . $_SERVER['HTTP_REFERER']);
?>
This should work fine.
Redirection with some delay say after 5 sec wait.
function js_redirect($url, $seconds)
{
echo "<script language=\"JavaScript\">\n";
echo "<!-- hide code from displaying on browsers with JS turned off\n\n";
echo "function redirect() {\n";
echo "window.parent.location = \"" . $url . "\";\n";
echo "}\n\n";
echo "timer = setTimeout('redirect()', '" . ($seconds*1000) . "');\n\n";
echo "-->\n";
echo "</script>\n";
return true;
}
js_redirect("http://www.exapmle.com",5); // Redirect after 5 sec
you forget to use
ob_start();
in first of your code :D
elsewhere you can use js in this case , for example :
echo 'javascript:window.location="http://example.com";';