php
  • if-statement
  • preg-replace
  • 2013-01-20 2 views 1 likes 
    1

    다음과 같이 문자열을 분할하는 데 문제가 있습니다. 텍스트로 유지할 텍스트는 무엇입니까? 앵커로 변환 할 링크는 무엇이며 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 없습니다 :

    나는 이런 식으로 뭔가와 함께했다.

    +0

    [**'preg_replace_callback()'**] (http://php.net/manual/en/function.preg-replace-callback.php) 기능을 확인하십시오. – inhan

    답변

    2

    사용

    preg_replace('/([^\'"])((ht|f)tps?:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i', 
        '$1<A target="_blank" href="$2">$2</A>',$string); 
    

    그리고 이를 넣어 URL이 패턴IFRAME 이전 진술 A 태그 배치 문.

    /*** make all the IFrame links ***/ 
    $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 URLs links ***/ 
    $string = preg_replace('/([^\'"])((ht|f)tps?:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i', 
         '$1<A target="_blank" href="$2">$2</A>',$string); 
    

    how it works을 참조하십시오.

    +0

    Yeey! 답변 해 주셔서 감사합니다. 제게 많은 도움이되었습니다. 너는 천재 야! –

    0

    당신은 정규식 접근 사용할 수 있습니다

    if (preg_match("/http://(?:www\.)?youtu(?:be\.com/watch\?v=|\.be/)(\w*)(&(amp;)?[\w\?=]*)?/i", $link)) 
    { 
        // It's a Youtube link! 
    } 
    

    을하지만이 방법 사용할 수 있습니다

    $parsedlink = parse_url($link); 
    
    if ($parsedlink['host'] == 'www.youtube.com') 
    { 
        // It's a Youtube link! 
    } 
    
    +0

    안녕하세요 귀하의 빠른 응답을 주셔서 감사합니다, 나는 전에 이것을 시도했지만 아이디어가 하나 이상의 링크가 있고 모든 URL을 구문 분석 할 수 없다거나 나는 PHP에서 그렇게 경험이없는 이유를 모르겠다. –

    +0

    그래서 우선 문자열에있는 모든 링크를 가져 오기 위해 정규 표현식을 만들어야합니다. 그 다음 하나씩 수정하고 문자열을 다시 작성합니다. –

    관련 문제