php搜索并添加内容

时间:2011-05-30 21:11:46

标签: php regex wordpress search

在我的内容中,我有一个“短代码”来显示具有不同参数的视频,具体取决于我希望视频的外观。例如:高度,宽度,视频等。

我可以在php中编写什么来搜索[video {paramaters}]并在其后添加内容?

请注意,{parameters}始终不同。

1 个答案:

答案 0 :(得分:0)

好吧,当我需要做这样的事情时,我使用preg_replace或preg_replace_callback ..

function videofunction($input){

//do something here and then:
return $whatever;

}

$content =  preg_replace('%\[video(.*?)\]%e', 'videofunction("\\1")', $content);

然后你必须提出逻辑=)

编辑: 刚看完你的帖子...... 您希望保留标记,并在其后添加内容。 你可以用preg替换这样做..像这样:

function videofunction($input){

$input .= "whatever else";
return $input;
}

preg_replace('%\[video.*?\]%e', 'videofunction("\\0")', $content);

可能不是最好的方法,也许某种偏移捕获会更好?