preg_replace自定义短代码

时间:2011-09-10 10:57:34

标签: php preg-replace

我有以下代码:

{bookielink href="bet-at-home"} body content{/bookielink}

我希望得到以下内容:

<a href="index.php?bookie=bet-at-home">body content</a>

这是我的尝试:

$buffer = preg_replace("/.*{bookielink[^>]*}|.*/si", "<a>", $buffer);
$buffer = preg_replace("/.*{\/bookielink}|.*/si", "</a>", $buffer);

1 个答案:

答案 0 :(得分:1)

以下内容:

$buffer = 'foo {bookielink href="bet-at-home"} body content{/bookielink} bar';
echo preg_replace(
    '#{bookielink\s+href="([^"]*)"\s*}([^{]+){/bookielink}#i', 
    '<a href="index.php?bookie=$1">$2</a>', 
    $buffer
);

将打印:

foo <a href="index.php?bookie=bet-at-home"> body content</a> bar