Here is a quick example, using preg_replace_all
, to show how it works :
if $content
is declared this way :
$content = "blah blah blah.. {mediafile file=img.jpg}yadda yadda, listen to this:
{mediafile file=audiofile7.mp3}
and whilst your here , check this: {mediafile file=audiofile24.mp3}";
You can replace the placeholders with something like this :
$new_content = preg_replace_callback('/{mediafile(.*?)}/', 'my_callback', $content);
var_dump($new_content);
And the callback function might look like this :
function my_callback($matches) {
$file_full = trim($matches[1]);
var_dump($file_full); // string 'file=audiofile7.mp3' (length=19)
// or string 'file=audiofile24.mp3' (length=20)
$file = str_replace('file=', '', $file_full);
var_dump($file); // audiofile7.mp3 or audiofile24.mp3
if (substr($file, -4) == '.mp3') {
return '<SWF TAG FOR #' . htmlspecialchars($file) . '#>';
} else if (substr($file, -4) == '.jpg') {
return '<img src="' . htmlspecialchars($file) . '" />';
}
}
Here, the last var_dump
will get you :
string 'blah blah blah.. <img src="img.jpg" />yadda yadda, listen to this:
<SWF TAG FOR #audiofile7.mp3#>
and whilst your here , check this: <SWF TAG FOR #audiofile24.mp3#>' (length=164)
Hope this helps :-)
Don't forget to add checks and all that, of course ! And your callback function will most certainly become a bit more complicated ^^ but this should give you an idea of what is possible.
BTW : you might want to use create_function
to create an anonymous function... But I don't like that : you've got to escape stuff, there is no syntax-highlighting in the IDE, ... It's hell with a big/complex function.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…