Jump to content

Recommended Posts

Hi,

 

 

I am trying to take a string from a database and replace everything within {} with code... similar to how posting in a forum works.

 

 

so say I have "...Lorem ipsom {gallery:1} sit imet..."

it will take that string (from a DB) and replace "{gallery:1}" with "<?php gallery('1'); ?>". How can this be done? Or is there keywords I can search on to find the answer? Thank you in advance.

Link to comment
https://forums.phpfreaks.com/topic/210468-replace-text-in-string-with-php-code/
Share on other sites

You'll want to use regex, an example

function gallery($id)
{
     return "[sHOW GALLERY ID: $id]";
}

$str = '...Lorem ipsom {gallery:1} sit imet...';
$str = preg_replace('~\{gallery:([0-9]+)\}~ies', 'gallery($1)', $str);

echo $str;

Everything in the exec' function (echo or otherwise) gets placed above the entire string when echoed. is there a workaround for this? the gallery() function requires in a template.php that prints the javascript image gallery to the page and a button that opens the gallery. but the button is above the string on the page. Is this an expected side effect of the require?

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.