如何将所有youtube视频URL转换为java中的字符串嵌入代码



我有一个包含youtube URL的文本。我想将所有youtube的URL转换为嵌入代码。例如:

来自

  • 样本https://youtu.be/S-thTTqefls?t=60

  • 样品https://youtu.be/xcJtL7QggTI?t=60

* Sample
++++
<iframe width="560" height="315" src="https://www.youtube.com/embed/S-thTTqefls?start=60" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
++++

* Sample
++++
<iframe width="560" height="315" src="https://www.youtube.com/embed/xcJtL7QggTI?start=60" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
++++

第一种方法

我想在嵌入代码之间推送匹配的youtube URL。但我不知道我该怎么做?

> "++++n" + "<iframe width="560" height="315" src="" + How can I
> push URL here with following code?
> + "" frameborder="0" allow="autoplayencrypted-media" allowfullscreen></iframe>n++++"

字符串内容=readFileAsString(路径(;Pattern MY_Pattern=Pattern.compile("^(https?\:\/\/(?(www\.(?(youtube\.com|youtu\.?be(\/.+$"(;匹配器m=MY_PATTERN。匹配器(内容(;Content=m.replaceAll("++++\n"+"\n++++"(;

Content = Content.replace("youtu.be", "youtube.com/embed");
Content = Content.replace("?t=", "?start=");

第二种方法

我试图通过在循环中找到所有youtube URL并将嵌入代码放在子字符串方法中来放置嵌入代码。

"n++++n<iframe width="560" height="315" src=""44字符

https://youtu.be/S-thTTqefls?t=60最大34 chracter

for (int i = -1; (i = Content.indexOf("https://youtu.be", i + 1)) != -1; i++) {
Content = Content.substring(0, i) + "n++++n<iframe width="560" height="315" src="" + Content.substring(i, Content.length());
Content = Content.substring(0, i + 78) + "" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>n++++n" + Content.substring(i + 78, Content.length());
}
Content = Content.replace("youtu.be", "youtube.com/embed");
Content = Content.replace("?t=", "?start=");

但是有些https://youtu.beURL没有得到嵌入代码。但我有这样一个奇怪的输出。

> ++++ <iframe width="560" height="315" src="
> ++++ <iframe width="560" height="315" src="<iframe width="560" height="315" src="
> ++++ <iframe width="560" height="315" src=" https://youtube.com/watch?v=xcJtL7QggTI?t=60" frameborder="0"
> allow="autoplay; encrypted-media" allowfullscreen></iframe>
> ++++
> 
> " frameborder="0" allow="autoplay; encrypted-media"
> allowfullscreen></iframe>
> ++++
> 
> " frameborder="0" allow="autoplay; encrypted-media"
> allowfullscreen></iframe>
> ++++

问题是我没有想到的Regex模式。我已经进行了搜索,并尝试了所有正则表达式模式,发现了以下

此正则表达式模式可成功查找所有URL。https://stackoverflow.com/a/31726735/9134980

Pattern MY_PATTERN = Pattern.compile("((http(s)?:\/\/)?)(www\.)?((youtube\.com\/)|(youtu.be\/))[\S]+");
Matcher m = MY_PATTERN.matcher(Content);
StringBuffer sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, "++++n" + "<iframe width="560" height="315" src="" + m.group(0) + "" frameborder="0" allow="autoplayencrypted-media" allowfullscreen></iframe>n++++");
}
m.appendTail(sb);
Content = sb.toString();
Content = Content.replace("youtu.be", "youtube.com/embed");
Content = Content.replace("?t=", "?start=");

您可以使用Java的replaceAll()方法。它还使用CCD_ 4。

一个简单的解决方案可以是这样的:

String url = "https://www.youtube.com/watch?v=xcJtL7QggTI?start=60";
public String getEmbedString(String url){
String embed = "iframe width="560" height="315" src="@" frameborder="0"allow="autoplay; encrypted-media" allowfullscreen></iframe>";
embed.replaceAll("@", url);
return embed;
}

我还没有尝试根据您的代码调整我的解决方案,因为这看起来要简单得多。如果这是你想要的,请告诉我。

最新更新