preg_replace with custom functions
Moderator: General Moderators
preg_replace with custom functions
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...
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...
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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)feyd wrote:preg_replace_callback() is what you're looking to use.
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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: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().
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.
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]);
?>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.
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.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
*cough* preg_replace_callback() not preg_match_...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..
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().
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():
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.
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);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.