YourNameHere Posted September 10, 2010 Share Posted September 10, 2010 I have a function that is supposed to look into a string from a database and replace all "{Gallery:[0-9]}" with a php function that will print a button to the page that opens an image gallery. Now, I can get the button on the page and make it work with the javascript gallery. However, it places the html for the button before the text string that it searched though. For example: String from DB: "Please check out this {gallery:9}." Should print : "Please check out this [Gallery 9 Button]." Instead, it prints: "[Gallery 9 Button] Please check out this" Here is my code that I have. function print_button($id) { echo '<span class="gallery_btn ui-state-default ui-corner-all" id="openerForGallery'.$id.'">Gallery</span>'; } // print_button() takes 1 param: id of the gallery, then echos the nessesary javascript and then a button // for opening that gallery. function get_galleries_from_string($str) { $str = preg_replace('~\{gallery:([0-9]+)\}~ies', 'print_button($1);', $str); return $str; } I have simplified the functions for debugging purposes. And it works just without the fluff of the javascript functions. If you know of a better way to do this, I am all ears. Quote Link to comment https://forums.phpfreaks.com/topic/213030-preg_replace-odd-behavior/ Share on other sites More sharing options...
Adam Posted September 10, 2010 Share Posted September 10, 2010 It's because you're echoing out the string within print_button(), you should return it. Quote Link to comment https://forums.phpfreaks.com/topic/213030-preg_replace-odd-behavior/#findComment-1109512 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.