Php 调用函数PREG_REPLACE

Php 调用函数PREG_REPLACE,php,preg-replace,preg-replace-callback,Php,Preg Replace,Preg Replace Callback,具有安全性,还具有不推荐使用的功能;在find and replace中调用函数的最简单、最安全的方法是什么 有四个查找和替换模块可插入内容[album][/album]、[img][/img]、[youtube][/youtube]或[vimeo][/vimeo] 使用到目前为止我整合的功能,YouTube和Vimeo是一个不需要动脑筋的工具。这张专辑没有那么多。我想根据传递的参数调用函数 我试着把这个函数改成一个preg\u replace\u回调,这只是模拟了一切。还有其他选择吗 func

具有安全性,还具有不推荐使用的功能;在find and replace中调用函数的最简单、最安全的方法是什么

有四个查找和替换模块可插入内容
[album][/album]、[img][/img]、[youtube][/youtube]或[vimeo][/vimeo]

使用到目前为止我整合的功能,YouTube和Vimeo是一个不需要动脑筋的工具。这张专辑没有那么多。我想根据传递的参数调用函数

我试着把这个函数改成一个
preg\u replace\u回调
,这只是模拟了一切。还有其他选择吗

function FormatModules($text) {
    $find = array(
        '~\[album\](.+?)\[/album\]~s',
        '~\[img width=(.*?) height=(.*?) alt=(.*?)\](https?://.*?\.(?:jpg|jpeg|gif|png))\[/img\]~s',
        '~\[youtube\](.+?)\[/youtube\]~s',
        '~\[vimeo\](.+?)\[/vimeo\]~s'
    );
    $replace = array(
        'GenerateAlbum($1)', // call a PHP function
        '<img src="$4" width="$1" height="$2" alt="$3" />',
        '<iframe src="http://www.youtube.com/embed/$1"></iframe>',
        '<iframe src="https://player.vimeo.com/video/$1"></iframe>'
    );
    return preg_replace($find, $replace, $text);
}
函数格式模块($text){
$find=数组(
“~\[album\](.+?)\[/album\]~s”,
“~\[img宽度=(.*)高度=(.*)高度=(.*)高度=(.*)高度=(.*)高度=(.*)高度=(.*)高度=(.*)高度=(.*)高度=(.*))(https?:/*?。(?:jpg | jpeg | gif | png))\[/img\]~s',
“~\[youtube\](.+?)\[/youtube\]~s”,
“~\[vimeo\](.+?)\[/vimeo\]~s”
);
$replace=数组(
'GenerateAlbum($1)',//调用PHP函数
'',
'',
''
);
返回preg_replace($find,$replace,$text);
}

如果您想在多个替换上调用函数,或者希望设置脚本以备将来修改,以便可以在替换参数上调用函数,您可以考虑

否则,我会说做一个
preg\u replace\u callback()
,涉及
$find
$replace
的第一个元素,然后对其余元素运行
preg\u replace()
调用

代码:()

函数GenerateAlbum($match){

return“如果您想在多个替换上调用函数,或者希望设置脚本以备将来修改,以便可以在替换参数上调用函数,您可以考虑

否则,我会说做一个
preg\u replace\u callback()
,涉及
$find
$replace
的第一个元素,然后对其余元素运行
preg\u replace()
调用

代码:()

函数GenerateAlbum($match){

返回“
preg\u replace\u callback
出了什么问题?您能显示您尝试使用
preg\u replace\u callback
的代码吗?正常运行其余部分,使用callback方法单独运行相册。
preg\u replace\u callback
出了什么问题?您能显示您尝试使用
的代码吗preg_replace_callback
。正常运行其余部分,使用callback方法单独运行相册。
function GenerateAlbum($match) {
    return "<div class=\"album\>Do whatever: " . strtoupper($match[1]) . "</div>";
}

function FormatModules($text) {
    $text = preg_replace_callback('~\[album\](.+?)\[/album\]~s', "GenerateAlbum", $text);
    $find = array(
        '~\[img width=(.*?) height=(.*?) alt=(.*?)\](https?://.*?\.(?:jpg|jpeg|gif|png))\[/img\]~s',
        '~\[youtube\](.+?)\[/youtube\]~s',
        '~\[vimeo\](.+?)\[/vimeo\]~s'
    );
    $replace = array(
        '<img src="$4" width="$1" height="$2" alt="$3" />',
        '<iframe src="http://www.youtube.com/embed/$1"></iframe>',
        '<iframe src="https://player.vimeo.com/video/$1"></iframe>'
    );
    return preg_replace($find, $replace, $text);
}

echo FormatModules("[vimeo]test1[/vimeo]\n\n[album]test2[/album]\n\njust text\n\n[img width=50 height=100 alt='sumpin good']http://www.example.com/image.gif[/img]\n\n[youtube]test3[/youtube]");
<iframe src="https://player.vimeo.com/video/test1"></iframe>

<div class="album\>Do whatever: TEST2</div>

just text

<img src="http://www.example.com/image.gif" width="50" height="100" alt="'sumpin good'" />

<iframe src="http://www.youtube.com/embed/test3"></iframe>