0

i am replacing youtube url with youtube video using jwplayer.

$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;


        }

it is working well when youtube url in normal text. i am facing problem with if youtube url in href tag, there also it is replacing youtube video , i need to replace only urls other then the href tag

output of $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
)
Naisa purushotham
  • 905
  • 10
  • 18

1 Answers1

0

I am going to go out on a whim here and try to correct your first regex.

Your solution

Change your replacement regex to the one below, which matches only youtube URLs that are not in html tags.

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

If it does not work, post back and I'll examine your code further.

  • Disclaimer: Still fairly new to regex, if this is wrong, not working or blew up your computer, please don't shoot me with downvotes –  Jul 08 '16 at 07:13
  • not working, i have changed like preg_match_all('#(?<!\")((http:\/\/|https:\/\/)(www.)?(youtube\.com|youtu\.be)\/(watch\?v=|\?v=)([a-zA-Z0-9]+))+(?!\")#', $disp_question_desc, $urls); getting same results – Naisa purushotham Jul 08 '16 at 07:24
  • As far as I can see, it works: [https://regex101.com/r/pL7iC4/1](https://regex101.com/r/pL7iC4/1) In case you have meant something else, @Naisapurushotham, do let us know. – Maria Ivanova Jul 08 '16 at 07:33
  • I will offer to rewrite your whole code for you but I need very very precise instructions as to how the code should run. What should it do? –  Jul 08 '16 at 07:40