替换包含变量的占位符文本

时间:2009-09-17 04:19:14

标签: php

您好我在CMS的内容中有占位符文字,如下所示:

$content = "blah blah blah.. yadda yadda, listen to this:
{mediafile file=audiofile7.mp3}
and whilst your here , check this: {mediafile file=audiofile24.mp3}"

我需要用一些html替换占位符来显示swf对象来播放mp3。
如何从占位符中获取文件名的替换。

我认为regx模式是{mediafile file=[A-Za-z0-9_]}但是我如何将它应用于包含标记的整个变量?

非常感谢任何可以提供帮助的人,

4 个答案:

答案 0 :(得分:1)

以下是一个使用preg_replace_all来展示其工作原理的简单示例:

如果$content以这种方式声明:

$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}";

您可以使用以下内容替换占位符:

$new_content = preg_replace_callback('/\{mediafile(.*?)\}/', 'my_callback', $content);
var_dump($new_content);

回调函数可能如下所示:

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) . '" />';
    }
}

在这里,最后var_dump将为您提供:

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)

希望这会有所帮助: - )


当然,不要忘记添加支票和所有这些!你的回调函数肯定会变得有点复杂^^但是这应该让你知道什么是可能的。

BTW:你可能想用create_function创建一个匿名函数......但我不喜欢这样:你必须逃避一些东西,IDE中没有语法高亮,。 ..这是一个很大/很复杂的功能。

答案 1 :(得分:0)

仔细阅读正则表达式文档。

你的模式看起来有点偏。 {mediafile file =([^}] +)}可能就像你正在寻找的那样(你给的正则表达式不允许“。”)。

答案 2 :(得分:0)

你做那样的事情

$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
    );

您可以看到preg_replace_callback的手册。普通的preg_replace也可以工作,但可能会很乱。

答案 3 :(得分:0)

我原本以为你可以使用一个涉及json_decode的函数,但字符串需要用引号括起来,否则json_decode不会处理它们。所以如果你的占位符是写的:

{"mediafile" : "file" : "blahblah.mp3"}

您可以将我的示例代码从使用explode($song)更改为json_decode($song, true)并使用一个很好的键控数组来处理。

无论哪种方式,我都使用strtok函数来查找占位符,然后使用基本的字符串替换函数将找到的占位符的实例更改为html,这只是乱码。

strtok,就PHP文档所指出的那样,不使用正则表达式,所以这不仅更简单,而且还避免调用preg库。

最后一件事。如果您使用json语法,则必须在{}中重新包装占位符,因为strtok会删除它正在搜索的标记。

<?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;
?>
相关问题