2016-07-08 5 views
1

나는 youtube url을 jwplayer를 사용하여 youtube video로 바꾼다.다른 URL에서 문자열을 추출한 다음 href

$str='<a href="https://www.youtube.com/watch?v=6anwsDt8AhA"> sometext</a>https://www.youtube.com/watch?v=6anwsDt8AhAApr 19, 2015 - Uploaded by Go FreelancerThis feature is not available right now. Please try again later. Published on Apr 19, 2015. How to get anchor ...How to get anchor text/href on click using jQuery? - YouTube Video for get text from html content jquery other than anchor 4:54 https://www.youtube.com/watch?v=6anwsDt8AhA Apr 19, 2015 - Uploaded by Go Freelancer This feature is not available right now. Please try again later. Published on Apr 19, 2015. How to get anchor ...'; 


// $disp_question_desc=$content; 
     $disp_question_desc=str_replace("http", " http", $str); 
     //get list of youtube urls 
     preg_match_all('#\b(?:http(?:s)?:\/\/)?(?:www\.)??(?:youtu\.be\/|youtube\.com\/)[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $disp_question_desc, $urls); 
     if(!empty($urls[0])) 
     { 

      foreach($urls[0] as $key=> $youtubeurl) 
      { 
       $div_id=$div_container."_".$key; 
       $youtubeurl=str_replace("/","\/",$youtubeurl); 
       $youtubeurl=str_replace("?","\?",$youtubeurl); 
       $youtube_content= "<div class='youtube-videos-container' srcval='".$youtubeurl."'><div class='youtube-videos ".$div_id."' id='".$div_id."' ></div></div>"; 
       $disp_question_desc= preg_replace("/".$youtubeurl."/", $youtube_content,$disp_question_desc,1); 
       $youtubeurl=str_replace("\\","",$youtubeurl); 

        if (!preg_match("~^(?:f|ht)tps?://~i", $youtubeurl)) { 
        $youtubeurl = "https://" . $youtubeurl; 
        } 


      } 

echo $disp_question_desc; 


     } 

youtube url이 정상적인 텍스트 일 ​​때 잘 작동합니다. $의 난 단지 다음의 다른 URL href를 태그 HREF 태그 무비 URL이 존재 또한 무비 비디오를 대체 제가 교체해야하는 경우에 문제를 직면하고

출력 된 URL [0]

Array 
(
    [0] => https://www.youtube.com/watch?v=6anwsDt8AhA 
    [1] => https://www.youtube.com/watch?v=6anwsDt8AhAApr 
    [2] => https://www.youtube.com/watch?v=6anwsDt8AhA 
) 

답변

1

나는 여기에 변덕에 나가서 첫 정규 표현식을 고치려고 노력할 것이다.

귀하의 솔루션

는 html 태그에없는 경우에만 유튜브 URL을 일치 아래와로 교체 정규식을 변경

.

(?<!\")((http:\/\/|https:\/\/)(www.)?(youtube\.com|youtu\.be)\/(watch\?v=|\?v=)([a-zA-Z0-9]+))+(?!\")

작동하지 않는 경우, 다시 게시하고 내가 더 당신의 코드를 검사합니다.

+0

면책 조항 : 정규 표현식에 상당히 새것입니다. 이것이 틀리거나 작동하지 않거나 컴퓨터를 폭발시킨 경우 downvotes로 나를 쏘지 마십시오. –

+0

이 작동하지 않아 변경되었습니다. \t \t preg_match_all # (?

+0

내가 볼 수있는 한, 작동한다 : [https://regex101.com/r/pL7iC4/1](https://regex101.com/r/pL7iC4/1) 그렇지 않으면, @ Naisapurushotham, 저희에게 알려주십시오. –