Jump to content

Preg_replace (odd?) behavior?


YourNameHere

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/213030-preg_replace-odd-behavior/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.