Skip to content

Instantly share code, notes, and snippets.

@fael
Created December 21, 2011 20:38
Show Gist options
  • Save fael/1507594 to your computer and use it in GitHub Desktop.
Save fael/1507594 to your computer and use it in GitHub Desktop.
Converter textos em Imagens, Videos e Links
<?php
$str = 'This is an image: google.ca/images/srpr/logo3w.png<br>
YouTube: http://www.youtube.com/watch?v=V2b8ilapFrI&feature=related <br>
Stackoverflow: http://stackoverflow.com/';
function converterLinksEmVideos($arr){
if(strpos($arr[0], 'http://') !== 0){
$arr[0] = 'http://' . $arr[0];
}
$url = parse_url($arr[0]);
// images
if(preg_match('#\.(png|jpg|gif)$#', $url['path'])) {
return '<img src="'. $arr[0] . '" />';
}
// youtube
if(in_array($url['host'], array('www.youtube.com', 'youtube.com')) && $url['path'] == '/watch' && isset($url['query'])) {
parse_str($url['query'], $query);
return sprintf('<img src="http://img.youtube.com/vi/%s/2.jpg"/><iframe class="embedded-video" src="http://www.youtube.com/embed/%s" allowfullscreen></iframe>', $query['v'], $query['v']);
}
//links
return sprintf('<a href="%1$s">%1$s</a>', $arr[0]);
}
$str = preg_replace_callback('#(?:https?://\S+)|(?:www.\S+)|(?:\S+\.\S+)#', 'converterLinksEmVideos', $str);
echo $str;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment