Page 1 of 1

preg_replace

Posted: Sun Feb 27, 2005 5:26 pm
by jonemo
i build a very small parser. it reads a file and replaces some bb code into html and stuff like that. now i want to extend it so that it replaces patterns like for example

Code: Select all

<!-- header -->
with the contents of a file header.inc.php.

in started off with this line:

Code: Select all

$content = preg_replace ("/\<\!\-\- (&#1111;a-z]*) \-\-\>/", "$1", $content);
and thought, that

Code: Select all

$content = preg_replace ("/\<\!\-\- (&#1111;a-z]*) \-\-\>/", insert("$1"), $content);
and the function insert did the right. problem: if i do an echo in the function, it echos for example "header", but if i use the line

Code: Select all

include ($parameter . '.inc.php'
it is trying to include "$1.inc.php" and not "header.inc.php" (where parameter is the parameter of the function insert()).

what am i doing wrong and what is the right solution?

btw: i read about preg_replace_callback, but didn't quite get the explanation in the manual. is that what i need?

Posted: Sun Feb 27, 2005 5:32 pm
by feyd

Code: Select all

$content = preg_replace('#<!-- (&#1111;a-z]+) -->#ie', 'insert(''\\1'')', $content);
you could use preg_replace_callback though.. since you are just passing the found data..