how get site conent using php - php

For example my site is mysite.com. Here is source of this site:
<html>
<head>
<title>site</title>
//here is many javascript and css codes
</head>
<body>
<div>
<table border="1">
<tr>
<td><a href="somthing.html">Here is a text</td>
<td><img src="image.gif" alt="this is image"/></td>
</tr>
</table>
</div>
</body>
</html>
How can I using php get only text and image without all tags(javascript codes, links, tables and other)?
I want get only "here is a text" and "image.gif".

Use PHP cURL if the file is on the internet else you can use the file_get_contents() function if the file on the local machine.
To get rid of the extra tags you can use the code:
$contents - file_get_contents('file.html');
$contents = strip_tags( $contents, '<img>' ); //other than <img> you can specify more tags also
Alternatively you can use the DOM approach also.

Related

PHP / HTML: Import HTML file into another one, while merging duplicates

First of all, I know that there are many questions leading towards including HTML.
The thing is, when I include one HTML (1) file into another (2), using <?php include("1.html") ?>, and both files consist of something like this:
<html>
<body>
<div id="specific div">
<span id="span1">1</span>
</div>
</body>
</html>
Having two different spans in the same specific div - once I include one file into the other one, it would look like this:
<html>
<body>
<div id="specific div">
<span id="span1">1</span>
</div>
<div id="specific div">
<span id="span2">2</span>
</div>
</body>
</html>
while I want the contents of the specific div merged into one of them, instead of having to divs with the same id in the end:
<html>
<body>
<div id="specific div">
<span id="span1">1</span>
<span id="span2">2</span>
</div>
</body>
</html>
How do I achieve that?
EDIT: I found a different and less complicated solution for my specific situation. Therefore I can't really select the correct answer now, so I might select one if it gets enough upvotes.
You could use php's DomDocument::loadHTMLFile() function. With this you can load both of your files and merge them the way you like it.
If your file looks like you said, something like this:
<html>
<body>
<div id="specific div1">
<span id="span">bla bla bla</span>
</div>
</body>
</html>
You can use the DomDocument:
$dom1 = new DomDocument();
$dom1->loadHTMLFile("file_1.html", LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$dom2 = new DomDocument();
$dom2->loadHTMLFile("file_2.html", LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$element = $dom2->getElementById("specific div2")->firstChild;
$dom1->getElementById("specific div1")->appendChild($element);
$merged_html = $dom1->saveHTML()
So this would merge the contents of the div[id="specific div2"] to the div[id="specific div1"]
DOMDocument also supports xpath if you like it more than going through the nodes maually or selecting by id.
If you want to include one html file to another then you can either use iframe or can convert the files to php as well to use include.
Like if you want to include index2.html in index1.html then you can use below iframe code in index1.html:
index1.html
<iframe src="index2.html"></iframe>
Or you can convert your files to PHP and then simply include one file to other like below:
index1.php
<?php include('index2.php'); ?>
You can also use Server Side Include like below to include one html to another:
index1.html
<html>
<body>
<!--#include virtual="/index2.html" -->
</body>
</html>

Calling R Script from PHP Page

I have a Rscript which I want to call from my PHP page. It saves two plots in a particular location and those are need to be displayed on the browser. The script is running fine in R Studio and plots are being saved at their respective locations. However, no output is being shown on browser.
<?php
exec("C:\Program Files\R\R-3.2.2\bin\Rscript code.R");
?>
<html>
<body>
<img src="C:/EasyPHP-12.1/www/RLinl/myplot1.jpeg">
<br>
<img src="C:\EasyPHP-12.1\www\RLinl\myplot2.jpeg">
<br>
<img src="C:/EasyPHP-12.1/www/RLinl/myplot3.jpeg">
<br>
</body>
</html>
Is there any problem in this call to the script ? The script uses a dataset and no parameters are required to pass.
<html>
<body>
<img src="/RLinl/myplot1.jpeg">
<br>
<img src="/RLinl/myplot2.jpeg">
<br>
<img src="/RLinl/myplot3.jpeg">
<br>
the above code is assuming that your document root is www and the images are being generated from R

Displaying images in a row with TCPDF and PHP?

I am trying to display 3 images in a row using TCPDF from my PHP script. This is what I do:
I create some HTML which contains the img.
I am using TCPDFs writeHTML function to write the HTML to a file.
When I check the created file, the images are not next to each other, each one is a bit below the previous. It looks like stairs.
To see whats going on I debugged the script and copied the HTML string I give as param to the writeHTML function into a HTML file and opened it in a browser. It looks exactly the way I want in a browser. ONly the created PDF seems to be messed up.
This is the HTML:
<table>
<tbody>
<tr>
<td width="100">
<img width="75" height="75" style="float: left;" src="...">
</td>
<td width="200">
<h2> FOOBAR </h2>
<p>
<!--Those 3 are the problem -->
<img width="30" src="...">
<img width="30" src="...">
<img width="30" src="...">
</p>
</td>
</tr>
</tbody>
</table>
I am calling TCPDF like this:
$pdf->writeHTML($myHtmlFromAboveInAString, true, false, true, false, '');
Any ideas what might be wrong? I tried removing the p, doesnt change a thing.
Thanks!
NOTE: I just found the answer myself! Each image has to be in another tables column, like showh here: TCPDF: Writing multiple images after eachother using writeHTML renders images in a "staircase" shape instead of in a straight line

trouble getting knockout.js to work with PHP

I followed one of the basic tutorials on the knockout.js website and now I'm just trying to make it work on localhost. In the head, I have included two files: knockout.js and misc_form.js.
<head>
<script src="/knockout.js"></script>
<script src="/misc_form.js"></script>
</head>
I'm going to omit the code for misc_form.js because I didn't modify it and it works 100% on the live knockout.js tutorial.
Here's the HTML
//form.php
function display_form(){
?>
<table>
<thead><tr>
<th>Passenger name</th><th>Meal</th><th>Surcharge</th><th></th>
</tr></thead>
<!-- Todo: Generate table body -->
<tbody data-bind="foreach: seats">
<tr>
<td><input data-bind="value: name" /></td>
<td><select data-bind="options: $root.availableMeals, value: meal, optionsText: 'mealName'"></select></td>
<td data-bind="text: formattedPrice"></td>
<td>Remove</td>
</tr>
</tbody>
</table>
<button data-bind="click: addSeat">Reserve another seat</button>
<?
}
Edit - I changed the path to the files and now I'm getting the following error
Uncaught TypeError: Cannot read property 'nodeType' of null //in reference to knockout.js
Check the paths for your script files, those look strange. Try visiting http://localhost/Applications/XAMPP/xamppfiles/htdocs/misc_form.js in a web browser, do you see your JavaScript code?
From the urls, I'm guessing they should be:
<head>
<script src="/knockout.js"></script>
<script src="/misc_form.js"></script>
</head>
since htdocs is likely the folder which gets served up as /.

Trouble Image Mapping In Dreamweaver

Dreamweaver is generally pretty easy to image map in but I can't seem to get the crosshairs for the image mapping tool to work. I have version CS5.5.
<html>
<head>
<div id="top-header">
<div align="center">
<table width="1000" border="0" align="center" cellpadding="0" cellspacing="0" margin="20">
<tr>
<div id="top-image"><img src="pics/TopHeader.jpg"></div>
<tr>
<div id="nav-bar"><img src="pics/NavBar.jpg" name="NavBar" border="0" usemap="#NavBarMap" id="NavBar"></div>
</div>
</div>
</div>
</table>
</head>
</html>
Basically I'm trying to just click on the NavBar.jpg file in the design window, get the rectangle mapping cursor and just start making the map. For further clarity, this is a header.inc.php file that I'm using to include in all the pages on the site. Every time I go to click on the image it highlights the table code on the split screen.
Can you not image map within a table? I'm using the table for alignment purposes so I'd like to keep it in there if possible. Thanks.
I'm not exactly sure what you are shooting for in your navigation, but you can do the whole thing with CSS in a multitude of different ways. It will be easier to create, and easier to change later on if you find that you need to.
If you haven't created navigation in css before, this list will help you cover a lot of ground: http://www.alvit.de/css-showcase/css-navigation-techniques-showcase.php
EDIT:
This site also has some excellent tutorials to clear up any confusion: http://net.tutsplus.com/?s=css+navigation

Categories