【问题标题】:How can I convert all youtube video urls to embed code into string in java?如何将所有 youtube 视频网址转换为将代码嵌入到 java 中的字符串?
【发布时间】:2019-01-12 02:31:30
【问题描述】:

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

来自

* 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(Path); 模式 MY_PATTERN = Pattern.compile("^(https?\:\/\/)?(www\.)?(youtube\.com|youtu\.?be)\/.+$"); 匹配器 m = MY_PATTERN.matcher(Content); 内容 = m.replaceAll("++++\n" + "\n++++");

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

第二种方法

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

"\n++++\n&lt;iframe width=\"560\" height=\"315\" src=\"" 44 个字符

https://youtu.be/S-thTTqefls?t=60 最多 34 个字符

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.be URL 没有嵌入代码。但我有这样一个奇怪的输出。

> ++++ <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>
> ++++

【问题讨论】:

  • 文本是指文本文件吗?
  • 是的。我阅读了文本文件的所有内容并传输了评论字符串。
  • 我不确定你为什么在这里使用正则表达式。看起来您只想替换 * Sample 之后的行,不管它是什么,因此不需要正则表达式,至少根据您发布的内容。在这种情况下,只需遍历所有行,检查它是否为* Sample,如果是,则将一些boolean 值(如afterSample)设置为true。如果afterSample 是真正的读取行但写入其替换版本(用&lt;iframe ...&gt; 包围。
  • 我有不同的 youtube 网址,彼此的 id 不同。 ...watch?v=xcJtL7QggTI ...watch?v=thTTqefls 有没有不使用正则表达式的简单方法?
  • 但据我所知,您并没有修改这些 URL。那么,如果您知道这些 URL 在字符串中的位置(* Sample 之后的下一行),那么这里的正则表达式有什么意义呢?

标签: java regex


【解决方案1】:

问题是我没想到的正则表达式模式。我进行了搜索并尝试了所有正则表达式模式并找到了以下内容

此正则表达式模式成功找到所有 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=");

【讨论】:

    【解决方案2】:

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

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

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

    我没有尝试使我的解决方案适应您的代码,因为这看起来要简单得多。如果这是您要找的,请告诉我。

    【讨论】:

    • 此代码将所有 youtube URL 转换为相同的 URL。但是我的文字中有不同的网址。
    • 我认为@IamSousakotiris 提供了一个示例,说明您可以如何轻松地将您的网址转换为嵌入式网址,就像您问的那样。他没有说如何从文本文件中检索每个 url。您可以根据您的解决方案调整他/她的答案
    • 是的。我要求将 URL 转换为嵌入式 URL,并且我在程序中完成了其他过程。该代码会将所有不同的网址转换为该网址(youtube.com/watch?v=xcJtL7QggTI?start=60)。
    • 亲爱的朋友,您好像有点糊涂了。上面的代码没有将String url; 声明为最终代码。这意味着你应该这样做 THISString url = whatever_url_you_read_from_your_file;
    猜你喜欢
    • 2014-03-03
    • 2015-09-29
    • 1970-01-01
    • 2019-08-15
    • 2015-03-02
    • 1970-01-01
    • 2013-03-27
    • 2013-10-03
    • 2011-07-04
    相关资源
    最近更新 更多