I’m working on a CMS and would like to be able to drop dynamic content into the middle of text blocks. For instance, if i wanted to drop a photo gallery widget into the middle of a paragraph of text, I could do this
the quick brown fox {{gallery:7}} jumped over the lazy dog.
In that example, gallery would tell the application I want to insert a photo gallery, and 7 is the ID number of the gallery I want to show.
I’ve come up with this method to determine the type and ID, but it’s rather clunky. Anybody have a better way?
function parseText($string){
$pattern = "/{{(.*?)}}/";
preg_match($pattern, $string, $matches);
$new = explode(':',$matches[1]);
$type = str_replace('{{','',$new[0]);
$id = str_replace('}}','',$new[1]);
return str_replace($matches,myFunction($type,$id),$string);
}
$string = 'the quick brown fox {{gallery:7}} jumped over the lazy dog.';
$parsed = parseText($string);
echo $parsed;