调用函数内部的preg_replace函数中的函数 [英] calling function inside preg_replace thats inside a function
本文介绍了调用函数内部的preg_replace函数中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
函数bbcode($ Text)
{/ / $ Text = preg_replace(/ \ [video \](。+?)\ [\ / video\] /,embed_video($ 1),$ Text);
return $ Text;}
函数embed_video($ url){
if(preg_match(/ http:\ / \ / www.youtube.com\ / watch\?v =([0-9a-zA-Z -_] *)(。*)/ i,$ url,$ matches)){
return'< object width =425高度= 350 >。
'< param name =movievalue =http://www.youtube.com/v/'.$matches[1]。'/>'。
'< param name =wmodevalue =transparent/>'。
'< embed src =http://www.youtube.com/v/'.$matches[1].'&autoplay=0type =application / x-shockwave-flash wmode =transparentwidth =425height =350/>'。
'< / object>';
}
return $ url;
}
$ lolcakes =[video] http://youtube.com/id/xxxxxxpron [/ video];
$ lolcakesconverted = bbcode($ lolcakes);
问题在于它向我吐出一个错误。
解析错误:语法错误,意外的T_LNUMBER,期待T_VARIABLE或'$'
有什么想法可以在bbcode函数的preg_replace中调用embed_video? p>
谢谢!
解决方案
code> preg_replace()(请参阅模式修饰符)
return preg_replace(/ \ [video\ ](。+?)\ [\ / video \] / e,embed_video('$ 1'),$ Text);
告诉 preg_replace()
来处理第二个参数为PHP代码。
I have some code with the a structure similar to this
function bbcode($Text)
{ //$Text = preg_replace("/\[video\](.+?)\[\/video\]/",embed_video($1), $Text);
return $Text;}
function embed_video($url){
if (preg_match("/http:\/\/www.youtube.com\/watch\?v=([0-9a-zA-Z-_]*)(.*)/i", $url, $matches)) {
return '<object width="425" height="350">'.
'<param name="movie" value="http://www.youtube.com/v/'.$matches[1].'" />'.
'<param name="wmode" value="transparent" />'.
'<embed src="http://www.youtube.com/v/'.$matches[1].'&autoplay="0" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350" />'.
'</object>';
}
return $url;
}
$lolcakes = "[video]http://youtube.com/id/xxxxxxpron[/video]";
$lolcakesconverted = bbcode($lolcakes);
The problem is it spits an error back at me.
Parse error: syntax error, unexpected T_LNUMBER, expecting T_VARIABLE or '$'
Have any ideas on how i can call embed_video inside of the preg_replace of the bbcode function?
Thanks!
解决方案
You can use the "e" modifier on preg_replace()
(see Pattern Modifiers)
return preg_replace("/\[video\](.+?)\[\/video\]/e", "embed_video('$1')", $Text);
which tells preg_replace()
to treat the second parameter as PHP code.
这篇关于调用函数内部的preg_replace函数中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文