如何将链接文本交换为链接和youtube链接,并在php中嵌入代码youtube
如何交换包含链接和youtube url的文本以嵌入youtube视频:如何将链接文本交换为链接和youtube链接,并在php中嵌入代码youtube,php,regex,youtube,swap,Php,Regex,Youtube,Swap,如何交换包含链接和youtube url的文本以嵌入youtube视频: $text = " normal text here youtube : http://www.youtube.com/watch?v=xxxxxx link : http://stackoverflow.com and some normal text here too. "; 结果: youtube : <ifra
$text = " normal text here
youtube : http://www.youtube.com/watch?v=xxxxxx
link : http://stackoverflow.com
and some normal text here too.
";
结果:
youtube : <iframe width="529" height="315" src="//www.youtube.com/embed/YOUTUBE_URL_HERE" frameborder="0" allowfullscreen></iframe>
link : <a href="http://stackoverflow.com">http://stackoverflow.com</a>
some normal text here too.
}什么?你能解释一下你的功能是什么,你的目标是什么,交换链接是什么意思…?我的意思是我有一些链接帖子,它有文本,文本有普通文本、链接和youtube URL。我想将文本中的每个链接转换为href和每个youtube URL嵌入代码。但在将youtube url转换为嵌入代码后,该函数会将文本链接转换为href,因此在下一步中,它也会将youtube_url_中的链接转换为href,这就是问题所在。我认为最好使用ignore-iframe标记转换文本中的链接。
function clickableLinksANDyoutubeIframe($text) {
//swapping to embed youtube video
$text = preg_replace("/\s*[a-zA-Z\/\/:\.]*youtube.com\/watch\?v=([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i","<iframe width=\"529\" height=\"315\" src=\"//www.youtube.com/embed/$1\" frameborder=\"0\" allowfullscreen></iframe>",$text);
//swapping to url href
$text = preg_replace('/(?<!\S)#([0-9a-zA-Zذضصثقفغعهخحجدشسيبلاتنمكطئءؤرلاىةوزظأإآ_]+)/', '<a href="/hashtag/$1" class="hashtaglink">#$1</a>', $text);
$text = preg_replace('/(?<!\S)@([0-9a-zA-Zذضصثقفغعهخحجدشسيبلاتنمكطئءؤرلاىةوزظأإآ_-]+)/', '<a href="/$1" class="hashtaglink">@$1</a>', $text);
$text = nl2br($text);
//the result
return $text;