Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
173 views
in Technique[技术] by (71.8m points)

php - calling function inside preg_replace thats inside a function

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

try preg_replace_callback

return preg_replace_callback("/[video](.+?)[/video]/", 'embed_video', $Text);

function embed_video($matches)
{
  return $matches[1] . 'foo';      
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...