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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.