从字符串中提取url然后href

时间:2016-07-08 06:48:48

标签: php jquery regex preg-match-all jwplayer7

我正在使用jwplayer用youtube视频替换youtube网址。

$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在href标签中,还有它正在替换youtube视频,我只需要替换除了href标签之外的其他网址

$ urls [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 个答案:

答案 0 :(得分:1)

我会在这里突发奇想,试着纠正你的第一个正则表达式。

您的解决方案

将替换正则表达式更改为下面的正则表达式,该地址仅匹配不在html标记中的YouTube网址。

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

如果它不起作用,请回发,我会进一步检查你的代码。