다음과 같이 문자열을 분할하는 데 문제가 있습니다. 텍스트로 유지할 텍스트는 무엇입니까? 앵커로 변환 할 링크는 무엇이며 youtube에서 iframe 태그로 변환하는 링크는 무엇입니까? 가 iframe을하기 전에>가 "그냥, 내가 같이 앵커를 얻을되어 어떻게 작동하고if 문 preg_replace 함수에서
$string='This is a link www.dinamomania.net this is a http://www.youtube.com/watch?v=U9LB6qGvdpQ';
echo makelink($string);
function makeLink($string){
/*** make sure there is an http:// on all URLs ***/
$string = preg_replace("/([^\w\/])(www\.[a-z0-9\-]+\.[a-z0-9\-]+)/i", "$1http://$2",$string);
/*** make all URLs links ***/
$string = preg_replace('/([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i','<a target="_blank" href="$1">$1</A>',$string);
$string = preg_replace('/((http|ftp)\:\/\/)?([w]{3}\.)?(youtube\.)([a-z]{2,4})(\/watch\?v=)([a-zA-Z0-9_-]+)(\&feature=)?([a-zA-Z0-9_-]+)?/', '<iframe style= "margin:15px 0 15px 0;display:block;" width="500" height="300" src="http://www.youtube.com/embed/$7" frameborder="0" allowfullscreen></iframe>',$string);
/*** make all emails hot links ***/
$string = preg_replace("/([\w-?&;#~=\.\/]+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}[0-9]{1,3})(\]?))/i","<A HREF=\"mailto:$1\">$1</A>",$string);
return $string;
}
}
모든 것이, 그게 전부는 먼저 다음 앵커와에 링크를하고 있기 때문에 :
내 스크립트는 다음과 같습니다 .. 유튜브 형식에 대한 iframe이이 앵커로의 inframe 그래서
내가 찾고 있던 문은 다음과 같이 뭔가를 수행하는 경우 :
if(is link from youtube) {
// do the iframe part
} else {
// do the anchor part
}
하나를 도움/조언은 높이 평가 될 것입니다. 감사 !
if (preg_match("/((http|ftp)\:\/\/)?([w]{3}\.)?(youtube\.)([a-z]{2,4})(\/watch\?v=)([a-zA-Z0-9_-]+)(\&feature=)?([a-zA-Z0-9_-]+)?/", $string))
{
$string = preg_replace('/((http|ftp)\:\/\/)?([w]{3}\.)?(youtube\.)([a-z]{2,4})(\/watch\?v=)([a-zA-Z0-9_-]+)(\&feature=)?([a-zA-Z0-9_-]+)?/', '<iframe style= "margin:15px 0 15px 0;display:block;" width="500" height="300" src="http://www.youtube.com/embed/$7" frameborder="0" allowfullscreen></iframe>',$string);
} else {
$string = preg_replace('/([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i','<a target="_blank" href="$1">$1</A>',$string);
}
은 그러나 다시 한번 내 iframe이 부분을하고있어 아무것도 앵커 happends 없습니다 :
나는 이런 식으로 뭔가와 함께했다.
[**'preg_replace_callback()'**] (http://php.net/manual/en/function.preg-replace-callback.php) 기능을 확인하십시오. – inhan