I created a few PHP files for users of a popular hardware site to use to "Metro" their news posts. It works fairly well, you add the title of the article, links etc. and then it spits it out in Metro tile format.
Take a look: http://briandempsey.org.uk/Newstool/index.php
When the user submits, it uses the information provided to create the post. Now, I need to somehow use PHP or some other language to display the code that it generated underneath it so users can just copy and paste it. I'm not sure how to do this.
header('Content-Type: text/plain');
Since you're passing your form data using the method GET, you could instead pass it to a page that creates a url to pull the html from...
index.php will have the form as you've shown above and will post to urlCreator.php.
form.php can be deleted as it is not needed anymore, the magic will happen in the urlCreator.php file.
urlCreator.php (NEW) will have code in it like so:
<?php
// urlCreator.php will get variables passed to it from index.php
// Now create the url using the passed variables
$url = "http://briandempsey.org.uk/Newstool/form.php?title=" . $_GET['title'] . "&color=" . $_GET['color'] . "&Articlecontent=" . $_GET['Articlecontent'] //and so on to build the whole url
//Now get the pages html
$html = file_get_contents($url);
?>
Now that you have the html in a variable you can clean it using str_replace or manipulate it however you'd like.
<?php
// Still in urlCreator.php
// With the html in a variable you could now edit out the divs and echo the results
$html = str_replace("<div style=\"background-color: #008299; height: auto; width: 100%; margin-left: 10px;\">", "", $html); //removes the intro div
$html = str_replace("</div>", "", $html); //removes the closing div
//Finally echo out the html to the page for copy+paste purposes
echo $html;
?>
Related
I'm building a simple tool that generates some custom HTML based on data from the website.
After, we can simply publish this content to a Facebook page via the Facebook SDK (API).
The following PHP code is an excerpt of how the $content is set up:
$content = '';
$content .= '📍 The address<br>';
$content .= '🛀 2 bathrooms<br>';
Via some AJAX code, this $content is shown as HTML in a div on the page, where the user can further edit the texts.
Then, the user can click on 'share' and via AJAX the post is published to Facebook. I use the following PHP code to format the content before it is passed to the FB API:
$content = str_replace('<br>',chr(10),$content);
$content = html_entity_decode($content);
$content = strip_tags($content);
The problem is that the emojis are not showing on Facebook. When I test it with
$content = html_entity_decode('️');
Then the emoji is showing correctly, but it seems that it's not working because I get $content via AJAX/Jquery with
var content = $('#content').html();
And then pass it through AJAX.
So I suppose there is a formatting issue, but I can't wait a way to fix it..
EDIT: because the content is first shown on the page in a div, the emojis are turned into <img draggable="false" role="img" class="emoji" alt="📍" src="https://s.w.org/images/core/emoji/14.0.0/svg/1f4cd.svg">
After, my Jquery/AJAX code takes the contents of this div, so I assume it takes the above instead of the '📍'. But how can I work around this?
EDIT 2:
Passing
$content = '';
$content .= '📍 The address<br>';
$content .= '🛀 2 bathrooms<br>';
Directly to the Facebook API works and shows the emojis.
First using an other PHP function that adds the above code to a div via JQUERY/AJAX, and then getting the div's content using $('#content').html() and passing this does not work.
I found a working solution.
As I suspected, the problem was with Jquery's element.HTML(), which seems to format the data in some way.
I have now added a hidden input field, where I add and get the data via element.val() instead of element.html();
Also, to prevent the auto-formatting (perhaps caused by WordPress) of emojis into images, I have created some custom tags like {sun}, which are only replaced with their HTML codes (like ️) just before posting to Facebook.
I started learning php. How can I find video source url with php. Can someone tell me how to get video url with simple_html_dom? For example how to find a video source url from a website?
Thank you
First of all, you need to understand how you can fetch an HTML document on a site. Everything you see in the browser consists of HTML and CSS. When you fetch a HTML document from a page with PHP, you get whatever is on the screen at that moment. So if a content is loaded into the page later using an Ajax call like below, you can't get it directly from the page.
function loadVideoURL() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.status == 200) {
document.getElementById("videosrc").innerHTML = this.responseText;
}
};
xhttp.open("GET", "get_video_url.php", true);
xhttp.send();
}
In the current situation, suppose that the video urls included in the HTML document in the first place.
First Download latest version of simplehtmldom from here. This is the PHP Class to manipulate HTML in easy way and this library requires minimum PHP 5. Create a file which name is simplehtmldomparser.php in main directory of your project and include it using code below to your main php code.
<?php
include 'simplehtmldomparser.php';
?>
This code block will load the class to your main PHP file. Now, you want to fetch video url from the page.
<?php
$html = file_get_html('http://www.videos.com/');
/* This line will fetch HTML document from the site that you wrote. */
?>
Now, you have the html document in $html variable. You need find video tags in HTML document. For example, If fetched page uses a <video> tag for videos, you can get the video urls as follows.
<?php
foreach($html->find('video') as $element){
echo $element->src . '<br>';
}
?>
Here is another example that might work for your project. For example you want to jump another video in the website, fetch all <a> tags and get their hrefs and use the $html = file_get_html($newlyfetchedanchor); again. To fetch all <a> tags from the current $html use code below.
<?php
foreach($html->find('a') as $element){
echo $element->href;
}
?>
There are more useful functions in the class and you can find here.
I currently have a wordpress website where the template is fixed but I would like to have the content all stored into 1 file that I can use to update the content.
The reasoning behind this is that I will have many different websites with the same template but different content. This way, if I ever required changes, all I would need to do is edit this 1 content.html file.
I have attempted to make a single HTML page where there would be different divs for different variables of the website, however the entire text of the HTML file is showing on the wordpress website rather than the specific id DIV "homepagetitle".
How do I use the file_get_contents or anything similar to retrieve specific sections of information through to my php wordpress website?
THE HTML FILE:
<html>
<div id="homepagetitle">AGENT SUCCESS 2 </div>
<div id="other">othercontent</div>
</html>
MY WORDPRESS SITE PHP FILE:
$homepagetitle = file_get_contents('http://neil1.zxstudios.ca/wp-content/themes/fullredpin5/content.html #homepagetitle');
echo '<h1 class="intro-text">'.$homepagetitle.'</h1>';
That is not how file_get_contents works. file_get_contents gets all file contents for the the specified target source. From the looks of it, I think you're coming from jQuery, where the .get() method allows to fetch page fragments with the #-identitifer.
If you want to emulate fetching a page fragment (e.g. #homepagetitle), look into DOM document parsing methods available in PHP (http://php.net/manual/en/class.domdocument.php). In pseudo-code:
$file = file_get_contents('/path/to/file.html');
$domParser = new DOMDocument($file);
$element = $domParser->getElement('element-id-here');
$text = $element->text;
echo '<h1>' . $text . '</h1>';
If you don't want to do this parsing, you need to split the file to multiple files and fetch the contents for different parts one-by-one. If you'd split that HTML file to two parts, they don't even need to be HTML:
file1.txt:
AGENT SUCCESS 2
file2.txt:
othercontent
Then in WP templates:
echo '<h1>' . file_get_contents('http://example.com/file1.txt') . '</h1>';
echo '<div class="content">' . file_get_contents('http://example.com/file2.txt') . '</div>';
Also, you're in deep sh*t in case someone hacks your file system and replaces those files, which would result unwanted content displayed on all websites where those files are fetched and displayed on.
You're using WordPress. Have you considered creating a central WordPress installation and use its RSS feeds (or maybe the REST API as of WP 4.4) to display the content elsewhere?
i have an html email being generated by certain php functions and collected from several tables in the database and organized in an html layout..right now it is all in a preview.php page for testing the layout but when i need to send it to subscribers, i need to get the html code generated from this page only,and send that code in an email.and i mean by page source the one i see when i right click on the page and then click view source..so how do i get this page source ? or save it into a certain variable to use it ?
Option 1:
Use file_get_contents(), since it returns the file in a string:
$html = file_get_contents('preview.php')
Your whole html is now saved in the $html variable as a string.
Option 2:
If your preview.php contains some PHP processing, you can do this instead (so that the PHP codes get executed, and you still get the resulting html):
ob_end_clean();
ob_start();
include('preview.php');
$html = ob_get_contents();
ob_end_clean();
Again, your whole html is now saved in the $html variable as a string.
You should generate your html with PHP and then save it in a session variable before echoing it.
Something like
$html = <<<HTML
<html>
<-- Here you have the full html of the page -->
</html>
HTML;
session_start();
$_SESSION['html'] = $html;
echo $html;
Then when you want to send the email you simply do
$message = $_SESSION['html'];
What you want looks a bit weird to me (why not get it into a variable instead of echoing it?)
Anyway, have a look to the ob_start & ob_get_contents functions
The right way is to have preview.php do this:
$html = '';
$html .= '<div>';
$html .= 'Text within div';
$html .= '</div>';
// etc
echo $html;
// Do other stuff with $html
But if you just want the lazy way, leaving preview.php doing echo statements, do this:
ob_start();
// Make the HTML using echo
$html = ob_get_contents();
ob_end_clean();
I'm trying to parse a HTML page where the majority of the content is contained in javascript. When I use the Chrome development tools I can see that the div class I'm trying to grab the content from is called div class=doodle-image. However when I either view the page as a source or try to grab it with php:
<?php
include_once('simple_html_dom.php');
$html = new simple_html_dom();
$html->load_file('http://www.google.com/doodles/finder/2012/All%20doodles');
$doodles = $html->find('.doodle-image');
echo $html;
?>
It returns the frame of the page but contains none of the divs or content. How can I grab the full content of the page?
That's because the element is empty when your PHP client fetches it, Google is loading in a JSON-object with JavaScript to populate the list of doodles. It does a Ajax-request to this page, and probably you can too.