Parsing strings from file_get_contents

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ast3r3x
Forum Commoner
Posts: 95
Joined: Thu Aug 19, 2004 8:36 pm

Parsing strings from file_get_contents

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
ast3r3x
Forum Commoner
Posts: 95
Joined: Thu Aug 19, 2004 8:36 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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....
ast3r3x
Forum Commoner
Posts: 95
Joined: Thu Aug 19, 2004 8:36 pm

Post by ast3r3x »

Ok, I'll check them out, thanks for the help!
ast3r3x
Forum Commoner
Posts: 95
Joined: Thu Aug 19, 2004 8:36 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

yup.. regex still would help with that..
ast3r3x
Forum Commoner
Posts: 95
Joined: Thu Aug 19, 2004 8:36 pm

Post 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.
Post Reply