2

What is the best/safest way to store html mark up with php code in it into a php variable? Or is there a better solution rather than storing it into a variable?

EDIT: Sorry for not including what I am trying to do! I have a an article template that pulls all information (title/date/content/etc) from a database and loads it. I have a page where you submit an article and what I am trying to do is automate the file creation process. I want to create a file that is named the title of the article, then write the template code to it (hence the variable containing the template code and using fwrite()). I know I could just keep the template file on the website and copy it over/rename it, but if someone stumbles upon the template it is a complete mess, and I don't want to store it in plain text either.

7
  • 1
    Please describe what you're actually trying to achieve first. There is probably an easy way (for instance, Flexy/Smarty templates or something). Commented Jul 24, 2011 at 20:37
  • depends,what are you trying to accomplish? Commented Jul 24, 2011 at 20:37
  • storeing php in a php var to exe latter is the wrong approach 99.9999% of the time Commented Jul 24, 2011 at 20:39
  • edited for clarification Commented Jul 24, 2011 at 20:40
  • that would usually be done with a db, not a flat file system Commented Jul 24, 2011 at 20:42

4 Answers 4

4

I prefer something like this

$str = <<<EOD
Example of string with html <div>some sample text</div>
spanning multiple lines <span>span text!</span>
using heredoc syntax.
EOD;
Sign up to request clarification or add additional context in comments.

2 Comments

I used to use Heredoc's alot, but I've come to think them problematic. They are nifty, but I usually don't necessarily recommend them anymore; either double-quoted strings or use a template system like Flexy, Smarty, or POPHP (Plain Ol' PHP), or output buffering (for the not-faint-of-heart).
I think that it's the best way too. But I have a dream: that NetBeans applies syntax colours to HTML heredocs :-) NOTE: be careful if your heredoc is inside an indented code, the heredoc close code (EOD; in the sample) must be at begin, at column 1.
2

This will give a cleaner way then EDO...

<?php ob_start(); ?>
<div>HTML goes here...</div>
<div>More HTML...</div>
<?php $my_var = ob_get_clean(); ?>

2 Comments

This is exactly what I was looking for. Perfect for navbars/sidebars. Put all the links into one of these. Then use for both navbar and sidebar.
Interesting to be noted that this works this way only. I mean that it needs the php opening and closing tag on both ob_start() and ob_get_clean() lines.
1

If you're working with a public website, I would not recommend storing things like articles in files on your server. It's messy, security-iffy, memory-inefficient, and otherwise unorthodox.

This would probably be a great situation in which to use a MySQL database. I'll assume you know how to work with one using PHP, but let me know if you don't know how.

In this way, you could store the HTML in the database using PHP's htmlentities() function:

$var = htmlentities($var, ENT_QUOTES);

This way, all html characters (ex. "<", ">" and quotes as well) are encoded into the database safely. For example,

<strong>Here is HTML</strong> becomes &lt;strong&gt;Here is HTML&lt;/strong&gt;

That way, if your templates have any HTML, they can be easily and safely retrieved through mysql_query() and displayed.

Comments

0

I'd store it in a file, and then when someone wants to create a new article, load it with DOMDocument, replace the elements you need to replace, and use DOMDocument::saveHTML to dump the code to a file when you're done =)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.