0

I'm trying to run a function whenever there's a [%xxx%] (acting as a placeholder, if you will), e.g.:

Bla bla bla blabla. Blablabla bla bla.
[%hooray%]
Blabla hey bla bla. [%yay%] blabla bla.

I'm pretty much a PHP beginner, but I managed to crack my head through and come up with the following (yay to me - I somehow managed to understand the basics of regular expressions!):

$maintext = preg_replace("#\[%(.{1,20})%\]#", "display_products('$1')"), $maintext);
echo $maintext; 

I tried working with the e modifier, I tried using preg_replace_callback, and it all does somewhat work - but I don't need the function to run before I echo the $maintext variable.

Any help on this guys?

edit The regex isn't the issue - that's working fine. I just need to figure out how to run the function only when I echo $maintext... preg_replace_callback runs the function immediately...

AKG
  • 2,936
  • 5
  • 27
  • 36
  • Try the %s and %d/%f modifier – qaisjp Jul 18 '12 at 08:29
  • Ugh, you should really stay away from `/e` but use `preg_replace_callback` instead. – ThiefMaster Jul 18 '12 at 08:29
  • @qaisjp how do I do that? I've got no issues with the regex - that's working fine. I just need to figure out how to run the function only when I echo $maintext... – AKG Jul 18 '12 at 08:35
  • yep. definitely don't use the `/e` modifier. It's considered very bad practice, and will be formally deprecated in the next version of PHP. – SDC Jul 18 '12 at 11:41

3 Answers3

0

Because PHP is a single threaded procedural scripting language1, it doesn't work in the way you want it to.

It is not possible to trigger the function to run only when you call echo2, what you need instead is an extra variable.

$newVariable = preg_replace_callback("#\[%(.{1,20})%\]#", function($matches) {
  // Do your thang here
}, $maintext);

// $maintext remains the same as it was when you started, do stuff with it here

// When you come to output the data, do...
echo $newVariable;

There are other approaches to this problem, like wrapping the above code in a function that can be called on demand and using the output buffering callback (see footnote #2), but reading between the lines I think all of this would be overkill for what is a relatively simple problem.

For the record, I think your regex would be better if you narrow the allowed content down a it, it will potentially match characters you don't want it to. I suspect that this would be better:

#\[%(\w{1,20})%\]#

This will allow only the characters a-zA-Z0-9_ in the placeholder.


1 Others may tell you PHP is now an object oriented language, but they are wrong. It still fundamentally works the same underneath and its still a scripting language, but now provides many OO-features.
2 It is possible to do something very close to this, but it is very rarely ever a good idea and is far too advanced for dealing with this problem.


EDIT

It is important to note that when using either method to pass replacements through a callback (e modifier or preg_replace_callback()) the callback function should return the new string, rather than outputting it directly.

This is because the code in any statement is executed from the inside out, and the "inner" echo statement (in the callback function) will be executed before the "outer" echo statement (on the line where preg_replace() is called, so the order in which things are output will be incorrect.

For example:

$str = "This is my string which contains a %placeholder%";

echo preg_replace_callback('/%(\w+)%/', function($matches) {
  echo "replacement string";
}, $str);
// Wrong - outputs "replacement stringThis is my string which contains a "

echo preg_replace_callback('/%(\w+)%/', function($matches) {
  return "replacement string";
}, $str);
// Right - outputs "This is my string which contains a replacement string"
DaveRandom
  • 87,921
  • 11
  • 154
  • 174
  • thanks @DaveRandom - I gave your code a try but I'm getting this error: **Catchable fatal error: Object of class Closure could not be converted to string in XXX** (on the same line as the preg_replace function)... – AKG Jul 18 '12 at 10:06
  • @AKG My bad, I left it as `preg_replace` instead of `preg_replace_callback`. Code above updated, try it again... – DaveRandom Jul 18 '12 at 10:09
  • @DaveRandom I#d like to know about this "possible to do something very close". Is there any way to get in contact? Or can you provide a link to that? It sounds very interesting and I've never heard of that before! – Mohammer Jul 18 '12 at 10:16
  • 1
    @Mohammer If you pass a callback to the first argument of [`ob_start()`](http://php.net/manual/en/function.ob-start.php) and a very low integer (minimum sensible value is `2`) to the second argument, the data will be passed through the callback function every time the length buffer exceeds the second argument (in bytes). Since you will rarely if ever output less than 2 bytes at a time, you are effectively calling the callback every time you output anything. – DaveRandom Jul 18 '12 at 10:47
  • @DaveRandom - thanks for your help, but I can't seem to wrap my head around this. Whenever I use preg_replace_callback or preg_replace with e modifier, it will run immediately - meaning that I don't have the function run at the place I want it. This is the pattern essentially: **Text** | **Function** | **More text** | **Function** – AKG Jul 18 '12 at 11:07
  • @AKG I'm not understanding why the point at which the function runs matters, as long as the end result is the same. Is there any chance you can show the code you have at the moment and the output it produces, and also the output you want to produce instead? – DaveRandom Jul 18 '12 at 11:09
  • @DaveRandom [This is the page I'm working on](http://www.aziedrachten.nl/restaurant/). As you can see, I can get the function to run - preg_replace finds 2 matches, hence 2 loops. All I need is to place the loops at the indicated positions. – AKG Jul 18 '12 at 11:17
  • @AKG I can see what you're trying to, can I see the source code of the page ([pastebin](http://pastebin.com/) is always good for that sort of thing) and I will show you how to do it ;-) – DaveRandom Jul 18 '12 at 11:21
  • @DaveRandom [This code produces the result you saw on the sample page](http://pastebin.com/5aPyUvtg) Thank you so much for guiding me through this. I've been trying to pick up PHP and Javascript over the past half a year, but I'm really more of the graphic designer :( – AKG Jul 18 '12 at 11:26
  • Ok right the proble is that your `display_products()` function outputs the data rather than returning it. Try [this](http://pastebin.com/CZmau55y) instead - may not be exactly right but I think it will be at least closer. Feel free to come back here with any questions you have about it. – DaveRandom Jul 18 '12 at 11:44
  • @DaveRandom You, my friend, are god sent. If you could edit the answer, then I'll vote your answer as the correct one. Thank you very much for your time and help! You helped out a noob. – AKG Jul 18 '12 at 12:03
  • @AKG Edited for your viewing pleasure... ;-) – DaveRandom Jul 18 '12 at 12:11
0

If you have a script that uses echo many times and you want the printed text modified, you can use output buffering:

ob_start();
/*
 * Lots of php code
*/

$maintext = ob_get_clean(); // now you capture the content in $maintext 

// than you modify the output
$maintext = preg_replace("#\[%(.{1,20})%\]#", "display_products($1)", $maintext);
echo $maintext;
Peter Adrian
  • 279
  • 1
  • 5
0

ob_start accepts a callback function, you can read more on this topic : Making all PHP file output pass through a "filter file" before being displayed

Community
  • 1
  • 1
Baptiste Placé
  • 386
  • 2
  • 5