0

i have a html text Contain YouTube links and i try to Replace these links and displayed videos directly on same page But there is something wrong in my code (Videos does not appear) , I also want modified pattern to looking for all youtube links youtube.com youtu.be together

my code :

$text = '<div id="post_message_395">
<a href="https://www.youtube.com/watch?v=FM0mkgv2JWk" target="_blank">https://www.youtube.com/watch?v=FM0mkgv2JWk</a><br>
<br>
Bla Bla Bla Bla Bla Bla 
<br>
<a href="https://youtu.be/FM0mkgv2JWk" target="_blank">https://youtu.be/FM0mkgv2JWk</a><br>
<br>
Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla 
</div>';
$reg_exUrl = '#(?:<a[^\<\>]+href=\")?(?:http://)?(?:[a-zA-Z]{1,4}\.)?youtube.com/watch\?v=(.{11}?)[^"]*(?:\"[^\<\>]*>)?([^\<\>]*)(?:</a>)?#';
preg_match_all($reg_exUrl, $text, $matches);
$links = $matches[2];

foreach ($links as $val) 
{
$turl= '<object width="425" height="350">
<param name="movie" value="'.$val.'&rel=0"></param>
<param name="allowFullScreen" value="true"></param><param  name="allowscriptaccess" value="always"></param>
<embed src="'.$val.'&rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" allownetworking="internal" width="580" height="420">
</embed>
</object>';  
$text = str_replace($val,$turl,$text); 
}
echo $text; 

outpot :

http://st3.pictub.club/2016/06/23/Capture123.png

omar dealo
  • 93
  • 9
  • 3
    why not just use `DOM` + `xpath` instead – Kevin Jun 23 '16 at 00:15
  • 2
    You shall not use regexes to parse HTML, see this: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 . You can parse a limited set of HTML with regexes: for example, if you first insert a new line before every occurrence of `href` then you have a specially formatted HTML which should be easier to parse by regexes. – grochmal Jun 23 '16 at 00:16
  • @Ghost Maybe because I do not know how i used DOM – omar dealo Jun 23 '16 at 00:20
  • @grochmal thanks but i don't understand anything from this link – omar dealo Jun 23 '16 at 00:21

0 Answers0