【问题标题】:How can I check whether the provided URLs is youtube or vimeo?如何检查提供的 URL 是 youtube 还是 vimeo?
【发布时间】:2018-12-04 17:46:56
【问题描述】:

我有这两个网址:

https://youtu.be/erwZDijlFAA

https://vimeo.com/262998843

现在我想通过 Twig 检查它。我该如何检查?

  {% if url %} <p>youtube</p> {% else %} <p>vimeo</p> {% endif %}

或者有什么方法可以检查提供的网址是 youtube 还是 vimeo ?

【问题讨论】:

标签: twig


【解决方案1】:

您可以使用使用starts with 运算符的简单版本,例如:

   {% if url starts with 'https://youtu.be/' %}
    YouTube.com
   {% endif %}

或者对于更复杂的条件,您可以通过matches 使用正则表达式,例如:

   {% if url matches '%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i' %}
    YouTube.com
   {% endif %}

RegEx 学分:https://gist.github.com/ghalusa/6c7f3a00fd2383e5ef33

More info in the doc about comparison operator here

希望有帮助

【讨论】:

    【解决方案2】:

    您可以使用下面的代码来隔离 twig 中的视频。

    {% set link_type = view.field.body.original_value %} // Fetching value from the body field
    
    {% if ('youtube' in link_type|render|render) %}
       youtube url
    {% else if ('vimeo' in link_typr|render|render ) %}
       vimeo Url
    {% endif %}
    

    【讨论】:

      猜你喜欢
      • 2011-09-30
      • 1970-01-01
      • 1970-01-01
      • 2010-11-24
      • 2019-12-15
      • 1970-01-01
      • 1970-01-01
      • 2018-03-28
      • 2020-10-09
      相关资源
      最近更新 更多