preg_replace with custom functions

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
User avatar
apoltix
Forum Newbie
Posts: 11
Joined: Fri Sep 30, 2005 3:27 pm
Location: Denmark

preg_replace with custom functions

Post by apoltix »

Hi! I've been using the DevNetworks Forums once before, and got just the answer I needed. Thanks for that! (This topic shouldn't be in the category regexp, right??)

Now I've got another problem. I have created a small CMS in PHP (using MySQL as database), and I wanted it to be possible for the system to recognize certain codes and call a function to handle a specific code, just like in MediaWiki (among others used on Wikipedia), e.g. where you have "{{stub}}" in the page, and the template "stub" is called. I've tried to use preg_replace to do this, though I can't seem to use functions in it, where the (in this case) "stub" is defined in the function (e.g. template('stub')) (which would be template($2) ??).

You know what I mean? A tiny string "encapsulated" in a easy recognizable code (e.g. between {{ and }}) should be recognized by the system using a regular expression, and a custom function should process that code, and return data and so on.

Thank you in advance...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

preg_replace_callback() is what you're looking to use.
User avatar
apoltix
Forum Newbie
Posts: 11
Joined: Fri Sep 30, 2005 3:27 pm
Location: Denmark

Post by apoltix »

feyd wrote:preg_replace_callback() is what you're looking to use.
Just tried that, didn't exactly know how it worked... With preg_replace I could replace strings with other strings and use some of the content using "(\W?)(.*?)" in the replaced string using $2, $4, etc. (And I'm new to regular expressions, therefore far from an expert on that subject)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The function (callback) is called with each found match for the pattern. It will receive an array of the match data as if it was coming from preg_match().
User avatar
apoltix
Forum Newbie
Posts: 11
Joined: Fri Sep 30, 2005 3:27 pm
Location: Denmark

Post by apoltix »

feyd wrote:The function (callback) is called with each found match for the pattern. It will receive an array of the match data as if it was coming from preg_match().
It returns all the matched data before everything else. And I can only use one function? Isn't there a way to use several different function that are made for several different strings (and codes)? What I'm looking for is something like this:

In text:
{{mod:testmodule}} => module('testmodule')
{{loadphp:test.php}} => loadphp('test.php')

I thought that the "testmodule" and "test.php" could be defined using numeric variables ($1, $2, etc.) in the replace-part of preg_replace, so it would be module($1) and loadphp($1) etc. I am using an array of patterns and replacements.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Perhaps the following can inspire you for a callback function...

Code: Select all

<?php
function mode($arg) {
	echo 'running mode with: ' . $arg;
}

function loadphp($arg) {
	echo 'running loadphp with: ' . $arg;
}

$parts = explode(':', 'mode:testmodule');
$func = $parts[0];
$func($parts[1]);
?>
User avatar
apoltix
Forum Newbie
Posts: 11
Joined: Fri Sep 30, 2005 3:27 pm
Location: Denmark

Post by apoltix »

Still don't get it...
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

What exactly don't you understand?

We've already shown you that with http://www.php.net/preg_replace_callback you can find and process all data that matches a given pattern with a custom function.

And then we have shown you how you can call a variable function, so you can choose a different one for each match..

modified preg_match_callback to preg_replace_callback *after* d11wtq's post.
Last edited by timvw on Sat May 06, 2006 7:27 am, edited 1 time in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

timvw wrote:What exactly don't you understand?

We've already shown you that with http://www.php.net/preg_match_callback you can find and process all data that matches a given pattern with a custom function.

And then we have shown you how you can call a variable function, so you can choose a different one for each match..
*cough* preg_replace_callback() not preg_match_...

FYI: Most things you can do with preg_replace_callback() you can do with the use of the "e" modifier in a standard preg_replace().
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

using a callback is safer than the evaluation modifier though.
User avatar
apoltix
Forum Newbie
Posts: 11
Joined: Fri Sep 30, 2005 3:27 pm
Location: Denmark

Post by apoltix »

I'll try experimenting with preg_replace_callback(). If it works, I'll post it here, if not, I'll keep asking questions, but thank you so far...
User avatar
apoltix
Forum Newbie
Posts: 11
Joined: Fri Sep 30, 2005 3:27 pm
Location: Denmark

Post by apoltix »

Okay, I think I've got it working now with preg_replace_callback(). I just don't like the way to treat the data. I used an array to replace things when I was using preg_replace():

Code: Select all

$text = "{{Test}} '''Fat text'''";
$stuff = array("/{{(\W?)(.*?)}}/" => "$2", "/'''(\W?)(.*?)'''/" => "<b>$2</b>");
$text = preg_replace(array_keys($stuff), array_values($stuff), $text);
will become: "Test Fat text".

Now I've gotta process the text a million times, but if it is what it takes, I suppose I'll accept it. Thanks for the help.
Post Reply