Page 1 of 1

Parsing strings from file_get_contents

Posted: Sat Sep 10, 2005 12:03 pm
by ast3r3x
I'm trying to make a page builder script so that I have a minimal amount of hard coded code. I have quickly run into problems though. I want one of my classes to do file_get_contents of a file that contains a basic beginning of each page.

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<title>swigg.net</title>
	
	<style type="text/css">
		@import "defvar::styles";
	</style>
</head>
<body>
Now just as an example, I want the defvar::styles to be parsed into what it's value is. I have this value , which I have set as an extends to the main class.

simplified class...

Code: Select all

class pbuild extends defvar
{
	$tagon = file_get_contents($file);
	var_dump($tagon);
}
What do I have to do to get the defvar::styles to parse so that when it's outputted, it has the correct value?

Posted: Sat Sep 10, 2005 12:12 pm
by feyd
you'll need a tokenizing engine, a string replacement engine, or use a regex engine to 1) pull the variable name to use out, 2) replace that with the correct value.

so you'll need to come up with rules as to how the engine finds the variable name first. Then work out a system whereby you can give it the variables in some fashion to do replacement with.

Posted: Sat Sep 10, 2005 1:30 pm
by ast3r3x
Don't mean to be a pest, but could you elaborate on that for me...possibly show me an example or point me in the right direction of something that does? I checked php.net's info about the tokenizer, but I don't really get it.

Posted: Sat Sep 10, 2005 2:01 pm
by feyd
a tokenizing engine basically is a loop that walks the string finding matching points. To speed the process, you can use strpos() and/or strstr()..

I personally prefer using regex, as it's more native to me :) There have been several threads (which are in the Regex forum here on DevNetwork) that talk about doing varialbe replacement....

Posted: Sat Sep 10, 2005 2:03 pm
by ast3r3x
Ok, I'll check them out, thanks for the help!

Posted: Sat Sep 10, 2005 2:26 pm
by ast3r3x
Ok, one more thing to say, and if I'm wrong, just give me the ASCII finger and I'll go checkout the regex forum some more. However, my problem isn't that I can't find the variables, it's that once I do I can't parse them. I mean technically yeah I could, but I want to do it a certain way.

I can easily find where defvar::styles is, but the thing is, I want that to BE the value of defvar::styles, not just the string of it. Is there a way to do that? I just want to make sure you understand what I am wanting to do so that I am not looking in the wrong place.

Posted: Sat Sep 10, 2005 3:09 pm
by feyd
yup.. regex still would help with that..

Posted: Sat Sep 10, 2005 3:49 pm
by ast3r3x
Well screw it, I don't know how to do what I'm doing, so I will just use an array as a look up table.