Page 1 of 1

preg_replace with custom functions

Posted: Thu May 04, 2006 2:16 pm
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...

Posted: Thu May 04, 2006 2:21 pm
by feyd
preg_replace_callback() is what you're looking to use.

Posted: Thu May 04, 2006 2:25 pm
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)

Posted: Thu May 04, 2006 2:34 pm
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().

Posted: Thu May 04, 2006 2:42 pm
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.

Posted: Thu May 04, 2006 3:17 pm
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]);
?>

Posted: Sat May 06, 2006 2:18 am
by apoltix
Still don't get it...

Posted: Sat May 06, 2006 7:08 am
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.

Posted: Sat May 06, 2006 7:16 am
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().

Posted: Sat May 06, 2006 7:25 am
by feyd
using a callback is safer than the evaluation modifier though.

Posted: Sat May 06, 2006 1:18 pm
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...

Posted: Sun May 07, 2006 2:00 pm
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.