preg_replace

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
jonemo
Forum Commoner
Posts: 28
Joined: Wed Feb 09, 2005 1:32 pm
Location: london, uk

preg_replace

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

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