Jump to content

Replace text in string with php code.


YourNameHere

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?

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.