I'm writing some code where I pull in the source code of my HTML page and print out on a new page the exact source code.
I need to add an extra that I can't get my head round how to do.
What I want, in the text of the source code, any text that is like the following:
<-- My admin code start -->
// code
// code
// code
<-- My admin code end -->
Should be omitted in the print out that I make.
The bit in the middle can be of any length.
ALSO: there will be several instances of the bits I want to leave out.
The fact that the bit in the middle is not fixed and the fact that there might be several instances of the text is what throws me - I don't know where to start.
I'm sure it's probably only 2 or 3 lines of code! Or is it a little more complex than this...?
Any help would be appreciated.
Thanks.
OM
blue-eye-labs
04-11-2008, 10:10 AM
you could use regex: preg_replace() and match the start and end patterns I suppose...
Or you could split the string up using a " " delimiter and write a loop function to find the end of the admin code...
I would recommend using regex.
TJ111
04-11-2008, 10:12 AM
If you get the file contents via fopen, fread, etc, then you can use string functions to replace the bits you want.
Using your example, here is something that should (untested) replace everyting between those two comments, including the comments themselves.
"Warning: preg_replace() [function.preg-replace]: Unknown modifier 'g' in /home/trade/public_html/duplicatingdirs/rishi/test1/mainFiles/source.php on line 14"
i'm looking up preg_replace now: but let me know if u know what the problem is...
thanks.
TJ111
04-11-2008, 10:44 AM
Oh, sorry, remove the "g" at the end of the first string. Also, it looks like your HTML chars are converted into HTML special chars, so you may need to change the "<" and ">" to $lt; and > .
OM2
04-11-2008, 10:45 AM
hmmm... trying to learn regular expressions in 5 minutes isn't possible.
i cant seem to find the answer. :(
Ahh, thats because your using the file function, which returns the file as an array, not a string. Also, I forgot the "s" flag. Also threw in the "i" flag to make it case insensitive.
WOW!
erm... i think i would have been here for a few more days without u!
i ran the code... and came up with the following errors:
Warning: filesize() [function.filesize]: stat failed for http://www.trademerchants.com//regularexpression/index.php in /public_html/regularexpression/source.php on line 5
Warning: fread() [function.fread]: Length parameter must be greater than 0 in /public_html/regularexpression/source.php on line 5
looking through ur code now and trying to understand.
let me know if u can fix the problem - i think we're nearly there!
THANKS!
TJ111
04-11-2008, 02:46 PM
Are there supposed to be two forward slashes in the url?
OM2
04-11-2008, 02:59 PM
erm... no... i did spot that before... id din't bother getting rid of the extra slash since it didn't seem to effect anything.
i got rid of the extra slash: still same error reported.
let me know what u think. thanks.
i tried to adjust this and use it in ur code.
it worked one way: but reported an error about the wrong number of arguements.
but: then i tried commenting out and redefining the funciton and make it take only one variable: it didn't like that.
BUT: i just realised... i DON'T want to print out th contents of the php file.
i want to print out the html that the php file makes. :( :( :( :( :(
TJ111
04-11-2008, 03:45 PM
I'm confused now lol. Show me what you have now.
OM2
04-11-2008, 04:05 PM
thanks for ALL the replies. u've been a star! :)
i got my solution (well... i think!).
i used the old code i had + ur guidance on what i was doing wrong + ur preg_replace function... and here's what we have:
surely that wont work?
the reason y it wasn't working in the first place was because i was applying the preg_replace to $line - which was only a line stored in an array (as opposed to being needed to be applied to the whole string in full).
in ur code... where does $wholeString come from?
i cant se it coming from any where?
let me know what u think.
thanks.
OM2
04-11-2008, 04:29 PM
ooops i take tthat back.
i was seeing it on a seperate line.
i think ur cod ei sbetter.
i'll use that. :)
OM2
04-11-2008, 04:31 PM
erm actually... that doesnt work after all. :)
i need to print out the source code.
whats being printed out is the html??
so the output is the website itself.
Works now :). I just changed "$lines as $line" to "$lines as &$line", so $line is just a reference to the item in the array $lines. If that makes sense.
Edit: make sure to change $url back.
OM2
04-15-2008, 07:29 PM
for some reason: not all automatic messages of new replies are sent: so i didnt know ud replied.
thanks for all the help.
webdeveloper.com
Copyright Internet.com Inc., All Rights Reserved.