calling function inside preg_replace thats inside a function - 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!

try preg_replace_callback
return preg_replace_callback("/\[video\](.+?)\[\/video\]/", 'embed_video', $Text);
function embed_video($matches)
{
return $matches[1] . 'foo';
}

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.

Related

preg_replace use reference as variable key [duplicate]

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!
try preg_replace_callback
return preg_replace_callback("/\[video\](.+?)\[\/video\]/", 'embed_video', $Text);
function embed_video($matches)
{
return $matches[1] . 'foo';
}
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.

Call Function PREG_REPLACE

With security and also a deprecated function; what would be the easiest and most secure way to call a function in a find and replace?
There are four find and replace modules which can be inserted within content [album][/album], [img][/img], [youtube][/youtube], or [vimeo][/vimeo].
Using the function I put together so far Images, YouTube and Vimeo were a no brainer. The Album no so much. I would like to call a function based on parameters that are passed.
I tried altering this function into a preg_replace_callback and that just mocks up everything. Is there any alternatives?
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);
}
If you wanted to call a function on more than one replacement or you wish to set up your script for future modifications so that functions can be called on the replacement parameter, you might entertain preg_replace_callback_array().
Otherwise, I'd say make a preg_replace_callback() involving the first elements of $find and $replace then run a call of preg_replace() on the remaining elements.
Code: (Demo)
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]");
Output:
<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>

youtube autoplay is not working with iframe

i have php function which generate youtube code to display video but auto play is not working properly
function get_youtube_embed($youtube_video_id = 0, $auto = 0) {
$embed_code = "";
if ($auto == 1) {
$embed_code = '<iframe width="589" height="342" src="http://www.youtube.com/embed/' . $youtube_video_id . '?autoplay=1" frameborder="0" allowfullscreen></iframe>';
}
else {
$embed_code = '<iframe width="589" height="342" src="http://www.youtube.com/embed/' . $youtube_video_id . '" frameborder="0" allowfullscreen></iframe>';
}
return $embed_code;
}
Thanks
Your code is alright, try doing it by hand, without Javascript. Most likely that the way to set autoplay is wrong in your code.
It appears that the autoplay does not always work in the newer
At the bottom of the embed option section on the youtube site, there is a checkbox allowing you to use the old code. Use this as your base.
Your code should look something like this:
$embed_code = '<object width="420" height="345"><param name="movie" value="http://www.youtube.com/v/' . $youtube_video_id . '?version=3&hl=en_US&rel=0&autoplay=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/' . $youtube_video_id . '?version=3&hl=en_US&rel=0&autoplay=1" type="application/x-shockwave-flash" width="420" height="345" allowscriptaccess="always" allowfullscreen="true"></embed></object>';
see: http://www.google.com/support/youtube/bin/answer.py?answer=171780&expand=UseOldEmbedCode#oldcode

How to parse embed object and get flashvars value from this using PHP?

I have this existing code does not work as I am trying to get flashvars , itemId value to be taken for some purpose. How can I parse and take it?
i need the value itemid = "OWYrYlg5VW9GZUI4UjVnMXFOUGsrQT09"
<?php
$result = '<embed src=\'http://www.test.com//test_1.swf\' quality="high" FlashVars="itemId=OWYrYlg5VW9GZUI4UjVnMXFOUGsrQT09&autoplay=0&duration=02:45&url=http://test.com" bgcolor="#ffffff" wmode="opaque" width="320" height="65" name="player" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>';
//preg_match("/\<embed.*itemId\=\"(.+)\".*\>.*\<\/embed\>/Us",$result,$match);
parse_str($result, $output);
print '<pre>';
print_r ($output);
?>
$result = 'your string';
$output = '';
preg_match('/itemId=([a-zA-Z0-9]+)/', $result, $matches);
$output = $matches[1];
preg_match("!<embed.*?itemId=([^&]+).*?>.*?</embed>!sm",$result,$match);

How to replace Youtube urls in text with embeded code (in PHP)?

This is what I've got so far:
<?php
$content = "word1 http://www.youtube.com/watch?v=yqfKe-67foQ&feature=related word2 http://www.youtube.com/watch?v=2vq7gDEn99Y&feature=related word3 http://www.youtube.com/watch?v=nW5HxgMYRto\nhttp://www.youtube.com/watch?v=8Uc2lpH0iZ0&feature=fvhl";
$pattern = '/http:\/\/www\.youtube\.com\/watch\?(.*)v=([a-zA-Z0-9_\-]+)(\S*)/i';
$replace = '<object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/$2&hl=en_US&fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/$2&hl=en_US&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object>';
$content = preg_replace($pattern, $replace, $content);
echo $content;
?>
I honestly have no idea why it doesn't work. Some help would be appreciated. Thanks.
You just have to make * non-greedy:
http:\/\/www\.youtube\.com\/watch\?(.*?)v=([a-zA-Z0-9_\-]+)(\S*)
See "Watch out for greediness".
Try this simple function for URL replacement
function youtube($string)
{
return preg_replace(
'#(http://(www.)?youtube.com)?/(v/|watch\?v\=)([-|~_0-9A-Za-z]+)&?.*?#i',
'<iframe title="YouTube video player" width="480" height="390" src="http://www.youtube.com/embed/$4" frameborder="0" allowfullscreen></iframe>',
$string
);
}
echo youtube('http://www.youtube.com/watch?v=VWsjWCt1PsQ');
echo youtube('http://youtube.com/watch?v=VWsjWCt1PsQ');
echo youtube('http://youtube.com/v/VWsjWCt1PsQ');
echo youtube('http://www.youtube.com/v/VWsjWCt1PsQ');
I would just create a string with a configured embed code. Link attribute could be #LINK#, width="#WIDTH#". Than replace all parameters with your own value. It could be much more comfortable to use. Or you can simply use a youtube embed code generator for that.

Categories