Reputation:
Hi I have placeholder text in my content from the CMS like this:
$content = "blah blah blah.. yadda yadda, listen to this:
{mediafile file=audiofile7.mp3}
and whilst your here , check this: {mediafile file=audiofile24.mp3}"
and i need to replace the placeholders with some html to display the swf object to play the mp3.
How do i do a replace that gets the filename from my placeholder.
I think the regx pattern is {mediafile file=[A-Za-z0-9_]}
but then how do i apply that to the whole variable containing the markers?
Thanks very much to anyone that can help,
Will
Upvotes: 0
Views: 1937
Reputation: 401032
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)
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.
Incidentally, 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.
Upvotes: 1
Reputation: 37065
I originally was thinking you could use a function that involved json_decode, but strings need to be wrapped in quotes or json_decode doesn't handle them. So if your placeholders were written:
{"mediafile" : "file" : "blahblah.mp3"}
you could change my sample code from using explode($song)
to json_decode($song, true)
and have a nice keyed array to work with.
Either way, I went with using the strtok
function to find the placeholders, and then a basic string replace function to change the instances of the found placeholders into html, which is just gibberish.
strtok
, so far as PHP docs indicate, does not use regex, so this would be not only simpler but also avoid a call to the preg library.
One last thing. If you do go with json syntax, you will have to re-wrap the placeholders in{}
as strtok
removes the tokens it is searching by.
<?php
$content = "blah blah blah.. yadda yadda, listen to this:
{mediafile file=audiofile7.mp3}
and whilst your here , check this: {mediafile file=audiofile24.mp3}";
function song2html($song) {
$song_info = explode("=", $song);
$song_url = $song_info[1];
$song_html = "<object src=\"$song_url\" blahblahblah>blah</object>";
return ($song_html);
}
$tok = strtok($content, "{}");
while ($tok !== false) {
if(strpos($tok, "mediafile") !== false) {
$songs[] = $tok;
}
$tok = strtok("{}");
}
foreach($songs as $asong) {
$content = str_replace($asong, song2html($asong), $content);
}
echo $content;
?>
Upvotes: 0
Reputation: 27313
you do something like that
$content = preg_replace_callback(
'|{mediafile file='([A-Za-z0-9_.]+)}|',
create_function(
// single quotes are essential here,
// or alternative escape all $ as \$
'$matches',
'return "<embed etc ... " . ($matches[1]) ."more tags";'
),
$content
);
you can see the manual of preg_replace_callback. Normal preg_replace also work but might be a messy.
Upvotes: 0
Reputation: 62894
Read the regex docs carefully.
your pattern looks a little off. {mediafile file=([^}]+)} might be ore like what you're looking for (the regex you gave doesn't allow for ".").
Upvotes: 0