Create a new page and add contents to it using PHP - php

This is going to be hard to explain so please ask me for clarification if anything confuses you.
So let's say that I am on createpage.php. Within this page, I have the following tasks.
Create a new page (file) using PHP via $file = fopen($pagename . '.php', "x").
Add premade content to this new page.
The method that I am taking to add new content to the new page is by storing the HTML content code into one variable called $newpagecontent. Then, I simply use fwrite($file, $newpagecontent).
The problem is though, that you can't store the whole of HTML/PHP file into $newpagecontent without breaking the double quotes. For example, $newpagecontent="echo("hello!")".
So the question is, is there a better way to add in HTML content to a newly created page? or is there a way to store the HTML code into a variable without breaking the syntax?
Thanks a lot

If you just want the contents of the other file that contains the HTML/PHP code, then try file_get_contents to read the contents and store it in the variable. Please see the documentation of this function for further details.

Related

How to gather code to be used in a htmlentities() function without rendering beforehand?

To put my question into context, I'm working on an entirely static website where 'post' pages are created by myself manually - there's no CMS behind it. Each page will require a <pre> <code> block to display code as text in a styled block. This could be very few - several which is why I'm trying to do this for ease.
Here's what I've done -
function outputCode($code) {
return "<pre class='preBlock'><code class='codeBlock'>".htmlentities($code)."</code></pre>";
}
The code works as expected and produces an expected outcome when it's able to grab code. My idea is to somehow wrap the code for the code block with this function and echo it out for the effect, fewer lines and better readability.
As I'm literally just creating pages as they're needed, is there even a way to create the needed code blocks with such function to avoid having to manually repeat all the code for each code block? Cheers!
EDIT:
I was previously using this function and it was working great as I was pulling code from .txt documents in a directory and storing the code for code blocks in a variable with file_get_contents(). However, now, I'm trying to get the function to work by manually inputting the code into the function.
Well. Wrapping the function input in ' ' completely slipped my mind! It works just fine now!
If I understand correctly, you want to re-use your outputCode function in several different PHP files, corresponding to posts. If yes, you could put this 1 function in its own file, called outputcode.php for example, and then do
include "outputcode.php";
in every post/PHP file that needs to re-use this function. This will pull in the code, from the one common/shared file, for use in each post/PHP file that needs it. Or maybe I'm misreading your last paragraph :(

wordpress style shortcodes in Ckeditor / PHP

I have built a custom CMS. Recently, I added the ability to create pages dynamically. I used CKEditor for the content of these pages.
I would also like to run some php functions that may be included in the content of the page stored in mysql.
I DO NOT want to store actual PHP code in the database, but rather function names perhaps. For example, in a page stored in the database I may have.
<?php //begin output
hello world!
check out this latest news article.
news($type, $id);
//end output
?>
What is the best way to find and execute this existing function without using EVAL if its found in the output? I was thinking along the lines of wordpress style short codes. Maybe [[news(latest, 71]] ? Then have a function to find and execute these functions if they exist in my functions.php file. Not really sure the best way to go about this.
I'm not searching for any code answers, but more of a best practice for this type of scenario, especially one that is safest against possible injections.
I found a solution from digging around and finding this thread
How to create a Wordpress shortcode-style function in PHP
I am able to pass short codes like this in CKEditor
[[utube 1 video_id]]
Then, in my page that renders the code:
print shortcodify($page_content);
using this function:
function shortcodify($string){
return preg_replace_callback('#\[\[(.*?)\]\]#', function ($matches) {
$whitespace_explode = explode(" ", $matches[1]);
$fnName = array_shift($whitespace_explode);
return function_exists($fnName) ? call_user_func_array($fnName,$whitespace_explode) : $matches[0];
}, $string);
}
If the function name exist (utube) it will fire the function.
Only problem Im having at the moment is not matter where I place the [[shortcode]] in my editor, it always executes first.
For example, in CKEditor I put:
Hello world! Check out my latest video
[[utube 1 video_id]]
It will always put the text under the video instead of where it is in the document. I need to figure a way to have the short code execute in the order it is placed.

How to manually call MediaWiki to convert wiki text to HTML?

I have a MediaWiki installation and I'm writing a custom script that reads some database entries and produces a custom output for client.
However, the text are in wiki format, and I need to convert them to HTML. Is there some PHP API I could call -- well there must be, but what and how exactly?
What files to include and what to call?
You use the global object $wgParser to do this:
<?php
require(dirname(__FILE__) . '/includes/WebStart.php');
$output = $wgParser->parse(
"some ''wikitext''",
Title::newFromText('Some page title'),
new ParserOptions());
echo $output->getText();
?>
Although I have no idea whether doing it this way is a good practice, or whether there is some better way.
All I found is dumpHTML.php that will dump all your mediawiki ; or may be better API:Parser wiki text which tells :
If you are interested in simply getting the rendered content of a
page, you can bypass the api and simply add action=render to your url,
like so: /w/index.php?title=API:Parsing_wikitext&action=render
Once you add action=render it seems you can get the html page ; dont you think ?
hope this could help.
regards.

Can't pass a PHP variable to file_get_contents()

I am a newbie coder trying to build a simple web app using PHP. I am trying to send an HTML email that has a variable that will change each time it is sent. The code to initiate the email is 'email.php' and contains:
$body = file_get_contents('welcome/green2.html.php');
Within the 'green2.html.php' file, I have a variable called $highlight that needs to be populated. The $highlight variable is defined within the 'email.php' file. I had tried to simply add within the 'green2.html.php' file, however it is not being parsed. I get a blank space where the variable should be when it is output.
Also, I have done an include 'welcome/green2.html.php' within the 'email.php' file. When I echo it, the $highlight var is shown on the resulting page, but not if I echo $body.
Any help would be much appreciated!
Have you tried the str_replace function? http://php.net/manual/en/function.str-replace.php.
Add a placeholder in HTML (for instance #name# for name, #email# for email), and then use the string replace function once you've loaded the content of the file.
$bodytag = str_replace("#name#", $name, $myfile);
Loading a file via file_get_contents() will not cause it to be parsed by PHP. It will simply be loaded as a static file, regardless of whether it contains PHP code or not.
If you want it to be parsed by PHP, you would need to include or require it.
But it sounds like you're trying to write a templating system for your emails. If this is what you're doing, you'd be better off not having it as PHP code to be parsed, but rather having placeholder markers in it, and then using str_replace() or similar functions to inject variables from your main program into the string.
Hope that helps.
Use http://php.net/manual/en/function.sprintf.php put a %s in your code instead of the variable read the content and put the string into the sprintf with the variable you want to put that's it. Hope this will help.

How do I use php so that when a user presses a specific button, they can write to a specific .txt file?

Basically, as in other questions I've asked related to my php chat application, I am trying to get it so that there is a text field where $msg is displayed via msg.txt. Two users can communicate to another in this way. This would be easy if I wanted to use a simple include function. But I don't want to take all the trouble to make and upload all those pages to my server. So how can I have it where when the user, say named Aaron, clicks on a button titled Benjamin, and types to a file called aaronbenjamin.txt, and if Aaron wants to talk to another user, he can press on a button titled Chris, and type to a file called aaronchris.txt? And all from the same box and text field? Thanks, I appreciate it.
EDIT: This is my code-
http://key-mail.zxq.net/msg.txt
Well, you should learn about using forms with PHP, with regards to your comment. If you read that tutorial fully, it should answer all of your questions about forms and php.
As far as getting it to write to a different text file, since you seem worried about "uploading all those pages", you'll be happy to know there's an easy solution!
There's a function called file_put_contents, which will create or write into a file.
Since you're new(ish) to PHP, here's an example:
<?php
$file = 'hello.txt';
$text = 'Hello World!';
file_put_contents($file, $text);
?>
This puts the contents of the $text variable into the file with the name stored in $file.
Reading from a file is similarly easy, with file_get_contents.
Assuming the file hello.txt exists from before and has the same contents, you can use the following code to read from the file and output its contents:
<?php
$file = 'hello.txt';
echo(file_get_contents($file));
?>
That will show the contents of $file.
Now, moving into the specifics of your question, if your form sets a "to" and a "from" GET variable where "to" is your username already, then the following code would write the value in a "message" GET variable into the file based on the pattern you gave:
<?php
$to = addslashes($_GET['to']);
$from = addslashes($_GET['from']);
$msg = addslashes($_GET['message']);
//addslashes is used as a small security measure
$file = $to . $from . '.txt';
file_put_contents($file, $msg);
?>
This fetches our variables from the GET array, sanitizes them to some extent, sets the file name, and writes the message to the file.
If you have any questions about specific parts of this or you'd like me to go into more detail, please feel free to ask!
This is how to write to a file:
http://php.net/manual/en/function.fopen.php
You're better off to use a database tho for sure.
To make different links do different things, you can use AJAX as you suggested or you can use GET variables to route functions on the PHP side. The latter is easier but means you will need to reload the page after the user presses the button.
here's a little demo:
click here
then at the top of the page in php:
if($_GET['clicked']==1){--write to file1---}
else if($_GET['clicked']==2){--write to file2---}
Hope it helps

Categories