Hi All,
I’m working on a bulletins board where I have to create a script that have to be ‘intelligent’ :
- embed Youtube video if the bulletin message contains a link to a youtube video
- insert a thumbnail of a website if the bulletin message is a link BUT not Youtube
I have the solution for both BUT separately…
For the 1st :
preg_match('#(http://www.youtube.com)?/(v/([-|~_0-9A-Za-z]+)|watch\\?v\\=([-|~_0-9A-Za-z]+)&?.*?)#i', $bulletin_msg, $text);
$hypertext = "<br />";
$newString = preg_replace('#(http://www.youtube.com)?/(v/([-|~_0-9A-Za-z]+)|watch\\?v\\=([-|~_0-9A-Za-z]+)&?.*?)#i', $hypertext, $bulletin_msg);
echo nl2br($newString);
preg_match_all('#(http://www.youtube.com)?/(v/([-|~_0-9A-Za-z]+)|watch\\?v\\=([-|~_0-9A-Za-z]+)&?.*?)#i',$input,$output);
foreach ($output[4] AS $video_id) {
if (!isset($video[$video_id])) {
$video[$video_id] = true;
$embed_code = '<object width="280" height="170"><param name="movie" value="http://www.youtube.com/v/'.$video_id.'&hl=en&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'.$video_id.'&hl=en&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="280" height="170"></embed></object>';
echo $embed_code.'<br>';
}
}
so the hyperlink is replaced with a line break and the video is embedded.
The solution for the 2nd issue is :
<?
$bulletin_msg = $bulletins_rows['message']; // from a database
$input = $bulletin_msg;
preg_match('/(http:\\/\\/[^\\s]+)/', $bulletin_msg, $text);
//$hypertext = "<a href=\\"". $text[0] . "\\" target='_blank' class='text'>" . $text[0] . "</a>";
$hypertext = "";
$newString = preg_replace('/(http:\\/\\/[^\\s]+)/', $hypertext, $bulletin_msg);
if ($text[0] != "") {
?>
<script type="text/javascript">wsr_snapshot('<?=$text[0];?>', 'my_websnapr_key', 'Tiny');</script>
<?
}
echo nl2br($newString);
with this, hypertext is replaced with zero string and a thumbnail of the website is displayed.
My problem is when combining these 2… The regex ‘/(http:\/\/[^\s]+)/’ will replace the youtube link as well, and I’m having duplicates : a thumbnail of the video and the embedded player. I can’t figure out how to combine the preg_match to insert a thumbnail (link other than youtube) and embed video (in case it’s a youtube link)…
Any idea and help is highly appreciated!