Hopefully I can explain this well enough to get an answer...
I'm trying to create a basic template parser to use in one of my scripts - the idea is that it will load in a text file that looks something like this:
Code: Select all
<html>
<head>
<title>Title goes here</title>
</head>
<body>
<!-- BEGIN type="my_paragraph" -->
<p>{my_paragraph}</p>
<!-- END -->
</body>
</html>
and I then use this code to extract the value between the
type="" attribute:
Code: Select all
preg_match('#<!-- BEGIN type="(.*?)" -->#', $this_line_of_code[$i], $matches);
$name_of_type = '{' . $matches[1] . '}';
What I now want to do is use preg_replace to replace the
{my_paragraph} text with a value from an array called
my_paragraph[]. With the code I've created so far (which I won't post because it's a bit of a mess at the moment) I can generate as many paragraphs as there are values in the
my_paragraph[] array but I don't want to hard-code this value when I use preg_replace, I want to be able to use whatever value happens to be between the
type="" attribute as the array name.
At the moment, my preg_replace code looks like this:
Code: Select all
$my_parsed_code[$line++] .= preg_replace("/$name_of_type/", $my_paragraph[$i], $this_line_of_code[$i]);
but what I need to do is replace the
$my_paragraph[$i] part of the function with a 'generated' version based on whatever
type="" contains. I tried this:
Code: Select all
$test_value = '$' . "$matches[1]" . '[' . "$i" . ']';
$my_parsed_code[$line++] .= preg_replace("/$name_of_type/", "$test_value", $this_line_of_code[$i]);
but it doesn't interpret
$test_value it just outputs it as-is where the paragraph text is supposed to appear. I figure there's probably some really clever tokens/runes that I need to do what I want but I don't know what they are and/or whether preg_replace can work in this way.
Any help for this rather tedious and involved question would be gratefully received
Mecha Godzilla