2016-07-08 27 views
1

私はyoutube videoを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

免責事項:これが間違っている、動作していない、またはコンピュータを爆破した場合は、 –

+0

が動作していない、私は変更しました\t \t preg_match_all #(?<!\ ")((http:\/\/| https:\/\ /) ?v =)([a-zA-Z0-9] +))+(?!\ ")# '、$ disp_question_desc、$ urls);同じ結果を得る –

+0

私が見る限り、それは動作します:[https://regex101.com/r/pL7iC4/1](https://regex101.com/r/pL7iC4/1)そうでなければ、@ナイスプール、私たちに知らせてください。 –

関連する問題