qhiiyr Posted October 22, 2008 Share Posted October 22, 2008 Hello, I am trying to write a function that reads a block of HTML. There are certain cues in the HTML that the function will replace with certain things (like a template). So far I've got that down, but I have a problem. There's a keyword that I want to replace with the call to a function, instead of just a variable. So like, for example, if the keyword was "apple", and I had the HTML code block I really would like an apple right now. then PHP would print out "I really would like an ", and then call the function I want it to call, and then print out " right now." I could do a str_replace() to replace the keyword ("apple") with the function call, and then just do an eval() on the whole block, but I don't want the block to be able to execute PHP code of its own. Any hints as to how I might go about this? Any help is appreciated. Quote Link to comment Share on other sites More sharing options...
trq Posted October 22, 2008 Share Posted October 22, 2008 Any hints as to how I might go about this? Any help is appreciated. Really depends on how your parser works already. It could be as simple as.... <?php function foo() { return "banana"; } $s = "I really would like an apple right now.\n"; echo str_replace('apple', foo(), $s); ?> Quote Link to comment Share on other sites More sharing options...
qhiiyr Posted October 22, 2008 Author Share Posted October 22, 2008 Oh wow, I had no idea you could call a function inside a str_replace(). Thanks a bunch! One question though, in your example, let's say you were to change it to <?php function foo() { return "banana"; } $s = "I really would like an apple right now.\n"; $code = str_replace('apple', foo(), $s); /* some other code */ echo $code; ?> Would foo() get called during the str_replace, or not until $code gets echoed? I need it to not be called until the code is printed out. Quote Link to comment Share on other sites More sharing options...
.josh Posted October 22, 2008 Share Posted October 22, 2008 foo would be called first. Then str_replace will be parsed and the result assigned to $code. Then whatever the result is will be echoed. Quote Link to comment Share on other sites More sharing options...
qhiiyr Posted October 22, 2008 Author Share Posted October 22, 2008 Crap. Is there any simple way to make foo not be called until the code is printed? Quote Link to comment Share on other sites More sharing options...
.josh Posted October 22, 2008 Share Posted October 22, 2008 Well, we're going to have to move from example code to your real code to figure that one out, because as it stands right now in the example code, the echo is already happening right after the whole str_replace/foo() thing. Quote Link to comment Share on other sites More sharing options...
CroNiX Posted October 22, 2008 Share Posted October 22, 2008 Crap. Is there any simple way to make foo not be called until the code is printed? Why does it matter? The result will be the same... Quote Link to comment Share on other sites More sharing options...
trq Posted October 22, 2008 Share Posted October 22, 2008 We really need to see how your implimenting this template engine / performance hog, but output buffering might assist. Quote Link to comment Share on other sites More sharing options...
qhiiyr Posted October 23, 2008 Author Share Posted October 23, 2008 Sorry for the long response time, but I think I found a workaround. That suggestion to use my function inside the str_replace really helped. Thanks a bunch for the help, guys! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.