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
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>
So I'm trying to add smileys to my website (bbCodes) but I can't figure out how to do it. I have all the smileys triggerwords and outputs in my database to make it easier to remove/add smileys.
This bit of code below does nothing... I do not get an error and it doesn't replace for example :happy: with the image happy.png
error_reporting(E_ALL);
ini_set('display_errors', 1);
include('/var/www/files/connect.php');
$SmileysQ = $DB->query("SELECT * FROM smileys");
$SmileysQ->setFetchMode(PDO::FETCH_ASSOC);
while($Smileys = $SmileysQ->fetch()) {
$text = preg_replace ('/\''.$Smileys['trigger'].'/is', '<img src="images/smileys/'.$Smileys['output'].'.png" height="15" width="15" />', $text);
}
What have I done wrong?
I think you have an unintended ' mark in the first argument of the preg_replace causing it to fail as it was searching for ':happy: not :happy:
The proper replace would more likely be:
$text = preg_replace ('/'.$Smileys['trigger'].'/is', '<img src="images/smileys/'.$Smileys['output'].'.png" height="15" width="15" />', $text);
example:
$text = ":happy: this is a test!";
$code = ":happy:";
$text = preg_replace ('/'.$code.'/is', '<img src="images/smileys/happy.png" height="15" width="15" />', $text);
print $text;
/*
outputs: <img src="images/smileys/happy.png" height="15" width="15" /> this is a test!
the extra ' gave me: :happy: this is a test!
*/
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);
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.
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.