Cześć.
Posiadam skrypt, który wyszukuje w stringu hiperłącza i zamienia je na wersję HTML. Skrypt zamienia wszystkie hiperłącza a nie tylko pierwszy z kolei, jak to niektóre robią.
Potrzebuję dopisać coś co sprawi, że wszystkie hiperłącza youtube zamieni na odtwarzacz flash. Niestety nie mogę znaleźć / napisać żadnego skryptu, który by nie kolidował z dobrze działającym już skryptem do translacji adresów.
Proszę o pomoc.
function formatUrlsInText($text){
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
preg_match_all($reg_exUrl, $text, $matches);
$usedPatterns = array();
foreach($matches[0] as $pattern){
if(!array_key_exists($pattern, $usedPatterns)){
$usedPatterns[$pattern]=true;
$pattern2 = substr($pattern, -3);
if($pattern2 == "gif" || $pattern2 == "peg" || $pattern2 == "jpg" || $pattern2 == "png"){
$text = str_replace($pattern, '<img src="'.$pattern.'" style="width: 100%;">', $text);
} else {
$text = str_replace($pattern, '<a href="'.$pattern.'" target="_blank">'.$pattern.'</a>', $text);
}
}
}
return $text;
}
function get_youtube_id($url) {
if (strpos( $url,"v=") !== false)
{
return substr($url, strpos($url, "v=") + 2, 11);
}
elseif(strpos( $url,"embed/") !== false)
{
return substr($url, strpos($url, "embed/") + 6, 11);
}
}
function formatYoutubeInText($text) {
$id = $this->get_youtube_id($text);
$width = '640';
$height = '385';
return '<object width="' . $width . '" height="' . $height . '"><param name="movie" value="http://www.youtube.com/v/' . $id . '&hl=en_US&fs=1?rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/' . $id . '&hl=en_US&fs=1?rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="' . $width . '" height="' . $height . '"></embed></object>';
}