View Full Version : Manage Website Template

11-16-2010, 01:31 AM
I'm working on a basic website manager. I've incorporated an open source html editor, but it doesn't know what to do with <!--#include virtual="/path/to/file.html" -->

So it ignores it and does not show it in the editor.. What I want to do is replace an instance like that with something like {INCLUDE=/path/to/file.html} in the $html variable (which contains the web page html that I'm editing).. so that appears (instead of the blank spot where the other code would be) when I load $html in the HTML editor. then when I submit the modified HTML in the editor, I want to replace {INCLUDE=/path/to/file.html} with the appropriate code <!--#include virtual="/path/to/file.html" --> in $_POST[html] so I can save it to a .html file.

Can someone help me.. or just point me in the right direction?


11-16-2010, 02:10 AM
Rather then a virtual include, which not all browsers support, why not a PHP include?

11-16-2010, 02:45 AM
My PHP application simply opens a .html file and puts the contents into the editor window.. then when the editor window is saved, it writes the contents back to the .html file.. a php include doesn't apply here

11-16-2010, 02:50 AM
Right... but then what is the virtual include for? The virtual include does the same thing PHP include does... read the contents, display in place... maybe I misunderstood your problem?

11-16-2010, 03:04 AM
I can try again..

I have a file manager. I want to open an html file and get the contents so I can display it in the editor window of my file manager.. but before I place the code in the editor window, I want replace all virtual include instances with a more friendly code like {INCLUDE=/path/to/file.html}

so I need to search the $html variable for all virtual includes and replace it with the other code.. then after I save the page, I need to replace all instances of {INCLUDE=/some/file.html} to the vritual include code so I can write it back to the .html file

I don't need to parse the data that's in the other file, I just need to replace the code that points to it..

11-16-2010, 05:26 AM
If its a constant replacement, str_replace will do, if its variable, a regex will do nicely there.

11-16-2010, 08:41 AM
If you're not going to actually help with the coding, why bother posting?

Is something like this what I need?

$html = preg_replace("<!--#include virtual=\"[a-zA-Z0-9_/-]\"-->", "{INCLUDE=\\0}", $html);

$html = preg_replace("{INCLUDE=[a-zA-Z0-9_/-]}", "<!--#include virtual=\"\\0\"-->", $html);

11-16-2010, 05:42 PM
If you're going to insult me, why should I help at all? I'm not in a habit of simply writing out code for people, as it often doesn't help them learn.

Yes, that'll do the job to switching between them, though your regex is wrong

$html = preg_replace('/\<!--#include virtual="([a-z0-9_/-]+?)"--\>/i', '{INCLUDE=$1}', $html);

$html = preg_replace('/\{INCLUDE=([a-zA-Z0-9_/-]+?)\}/i', '<!--#include virtual="$1"-->', $html);

11-17-2010, 03:10 AM
Sorry, at first I thought you were one of those people just making comments to get your post count up..

I do have a question about your code though, I'm trying what you posted..

$html = preg_replace('/\<!--#include virtual="([a-zA-Z0-9_/-]+?)"--\>/i', '{INCLUDE=$1}', $html);

I get this message:

Warning: preg_replace() [function.preg-replace]: Unknown modifier '-'

I've tried removing the - but then I get an error message about the ]

Is there something I'm missing?

11-17-2010, 03:26 AM
Is there something I'm missing?

Yes. Look at the delimiters. You can't just plonk an unescaped delimiter in the middle of the expression without it throwing a fit.

11-17-2010, 03:46 AM
You can't just plonk an unescaped delimiter in the middle of the expression without it throwing a fit.

Which delimiter are you referring to? There's several in there..

11-17-2010, 04:10 AM
There are exactly two delimiters in a valid regular expression. No more, no less. You have three.


11-17-2010, 04:25 AM
Well I stopped the error message from showing up with this line:

$html = preg_replace('/\<!--#include virtual="([a-zA-Z0-9_\/-]+?)"--\>/i', '{INCLUDE=$1}', $html);

but it still doesn't work like it should..

11-17-2010, 04:35 AM
Judging by your description in post one, you're going the wrong way round, so to cater for either scenario:

$html = preg_replace('~\<\!--#include virtual="([^"]+)"--\>~i', '{INCLUDE=$1}', $html);
$html = preg_replace('~\{INCLUDE=([^\}]+)\}~i', '<!--#include virtual="$1"-->', $html);