【问题标题】:Specifically convert only YouTube urls to iframe tags while converting BBCode urls to html elements专门将 YouTube 网址转换为 iframe 标签,同时将 BBCode 网址转换为 html 元素
【发布时间】:2019-04-04 20:15:29
【问题描述】:

我正在尝试将 youtube 视频链接转换为 iframe 以将它们嵌入到我的网站上,但我遇到了一个问题,因为我已经将 url 转换为锚标记并且它与 youtube url 冲突。

这是我的代码:

$string = 'https://www.google.com and https://www.youtube.com/watch?v=umFQckeDwEE';
$url = '~(\s|^)(https?://.+?)(\s|$)~im';
$string = preg_replace($url, '[url=$0]$0[/url]', $string);
$youtubeURL = '/\s*[a-zA-Z\/\/:\.]*youtu(be.com\/watch\?v=|.be\/)([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i';
$string = preg_replace($youtubeURL, '<iframe width=\"420\" height=\"315\" src=\"https://www.youtube.com/embed/$2\" allowfullscreen></iframe>', $string);

第一行得到的只是一个简单的 url,因为我使用的是 BBCode,它与 [img] 标签等其他内容冲突。 $youtubeURL 是获取所有 youtube 链接并将它们转换为 iframe,但由于第一次链接转换,它会导致 href 被放入 scr=。

【问题讨论】:

  • 志愿者,您可能会发现查看此 OP 的上一个问题以了解有关该问题的更多上下文会有所帮助。
  • 这可能很有价值:stackoverflow.com/a/48968340/2943403
  • 所以基本上对于每个你不想匹配包含在 BBCode 标签中的 url 的内容?
  • 我希望能够仅将 youtube 链接转换为 iframe,因此那些不包含任何 BBCode 或仅包含常规链接(如 google.com)的链接
  • 使用这些$url = '~(?&gt;\[[^\[\]]*\](*SKIP)(*FAIL)\s*)*\s*\Khttps?://.+?(?!\S)~'; $youtubeURL = '~(?&gt;\[[^\[\]]*\](*SKIP)(*FAIL)\s*)*\s*[a-zA-Z/:.]*youtu(be\.com/watch\?v=|\.be/)([a-zA-Z0-9\-_]+)([a-zA-Z0-9/*_?&amp;;%=.-]*)~';

标签: php regex youtube preg-replace bbcode


【解决方案1】:

从your previous question 开始,我已经开发并测试了一整套正则表达式,让您顺利上路。我承认这组模式可以进一步细化以更精简(更少的总模式),并且 youtube 验证模式可以更严格,但老实说我没有时间去钻研那些兔子洞。

在考虑 youtube 网址的复杂性/多样性时,您可能需要参考我从 StackOverflow 页面和互联网中提取的示例列表。 https://regex101.com/r/zinjze/1

代码:(Demo)

$bbcode = <<<BBCODE
Want a list?
[ul][li]Here is a video: https://www.youtube.com/watch?v=mUxt--mMjwA[/li]
[li]This is a [b]tagged[/b] video: [url]https://www.youtube.com/watch?v=u6MyOXk98DI[/url][/li]
[li]This is a [b]tagged & attributed[/b] video: [url=https://www.youtube.com/watch?v=8G2WzH4AKpE]Pearl Jam - Present Tense[/url][/li]
[li]Look at this:https://www.example.com/example?ohyeah=sure#okay this is a raw link[/li]
[li][i]No attribute[/i] bbcode url: [url]http://example.com/x1[/url][/li]
[li]A url with link and link text: [url=http://example.com/x2]x2[/url][/li]
[li]Image with \"ignorable" text: [IMG=https://upload.wikimedia.org/wikipedia/commons/thumb/b/be/Portrait_of_Julie_Bishop.jpg/220px-Portrait_of_Julie_Bishop.jpg]Julie Bishop[/IMG][/li]
[li]Image: [img=https://docs.joomla.org/images/3/37/Joomla-3D-Vertical-logo-light-background-en.png][/img][/li]
[li][quote]"I could either watch it happen or be a part of it."[/quote]
    - Elon Musk[/li]
[li][user=2943403]mickmackusa[/user][/li]
[li]Code: 
[code]while (\$noSuccess) {
    tryAgain();
    if (\$dead) break;
}[/code][/li]
[/ul]
BBCODE;

$search = array (
    '~\[b](.*?)\[/b]~is',
    '~\[i](.*?)\[/i]~is',
    '~\[u](.*?)\[/u]~is',
    '~\[ul](.*?)\[/ul]~is',
    '~\[li](.*?)\[/li]~is',
    '~\[user=(.*?)](.*?)\[/user]~i',
    '~\[url=https?.*?(?:[/?&](?:e|vi?|ci)(?:[/=]|%3D)|youtu\.be/|embed/|/user/[^/]+#p/(?:[^/]+/)+)([\w-]{10,12})].*?\[/url]~i',
    '~\[url]https?.*?(?:[/?&](?:e|vi?|ci)(?:[/=]|%3D)|youtu\.be/|embed/|/user/[^/]+#p/(?:[^/]+/)+)([\w-]{10,12}).*?\[/url]~i',
    '~\[url=((?:ht|f)tps?://[a-z\d.-]+\.[a-z]{2,3}/\S*?)](.*?)\[/url]~i',
    '~\[url]((?:ht|f)tps?://[a-z\d.-]+\.[a-z]{2,3}/\S*?)\[/url]~i',
    '~\[img=(.*?)].*?\[/img]~i',
    '~\[quote](.*?)\[/quote]~is',
    '~\[code](.*?)\[/code]~is',
    '~(?:<a.*?</a>|<img.*?</img>|<iframe.*?</iframe>)(*SKIP)(*FAIL)|(?:\bhttps?.*?(?:[/?&](?:e|vi?|ci)(?:[/=]|%3D)|youtu\.be/|embed/|/user/[^/]+#p/(?:[^/]+/)+)([\w-]{10,12}))\S*~i',
    '~(?:<a.*?</a>|<img.*?</img>|<iframe.*?</iframe>)(*SKIP)(*FAIL)|\bhttps?://.+?(?=\s|$)~im'
);

$replace = array (
    '<strong>$1</strong>',
    '<em>$1</em>',
    '<u>$1</u>',
    '<ul>$1</ul>',
    '<li>$1</li>',
    '<a href="../login/profile?u=$1" target="_blank">$2</a>',
    '<iframe width="640" height="360" src="https://www.youtube.com/embed/$1" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>',
    '<iframe width="640" height="360" src="https://www.youtube.com/embed/$1" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>',
    '<a href="$1" target="_blank">$2</a>',
    '<a href="$1" target="_blank">$1</a>',
    '<img src="$1"></img>',
    '<quote>$1</quote>',
    '<code>$1</code>',
    '<iframe width="640" height="360" src="https://www.youtube.com/embed/$1" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>',
    '<a href="$0" target="_blank">$0</a>'
);    

echo preg_replace($search, $replace, $bbcode);

未渲染输出:(如果您想查看渲染输出,可以将我的 sn-p 复制粘贴到http://phptester.net/)

Want a list?
<ul><li>Here is a video: <iframe width="640" height="360" src="https://www.youtube.com/embed/mUxt--mMjwA" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
<li>This is a <strong>tagged</strong> video: <iframe width="640" height="360" src="https://www.youtube.com/embed/u6MyOXk98DI" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></li>
<li>This is a <strong>tagged & attributed</strong> video: <iframe width="640" height="360" src="https://www.youtube.com/embed/8G2WzH4AKpE" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></li>
<li>Look at this:<a href="https://www.example.com/example?ohyeah=sure#okay" target="_blank">https://www.example.com/example?ohyeah=sure#okay</a> this is a raw link</li>
<li><em>No attibute</em> bbcode url: <a href="http://example.com/x1" target="_blank">http://example.com/x1</a></li>
<li>A url with link and link text: <a href="http://example.com/x2" target="_blank">x2</a></li>
<li>Image with \"ignorable" text: <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/b/be/Portrait_of_Julie_Bishop.jpg/220px-Portrait_of_Julie_Bishop.jpg"></img></li>
<li>Image: <img src="https://docs.joomla.org/images/3/37/Joomla-3D-Vertical-logo-light-background-en.png"></img></li>
<li><quote>"I could either watch it happen or be a part of it."</quote>
    - Elon Musk</li>
<li><a href="../login/profile?u=2943403" target="_blank">mickmackusa</a></li>
<li>Code: 
<code>while ($noSuccess) {
    tryAgain();
    if ($dead) break;
}</code></li>
</ul>

【讨论】:

  • 哇,你是个圣人!我从来没有见过有人在这里帮过我这么大的忙,我通常只在这里问问题,希望能得到一点帮助,但是哇,你应该得到某种奖牌。我能想到的唯一改进就是 twitch.tv 嵌入。我试图复制你的 youtube 代码,但不能完全正确。
  • 给我看一个 twitch url 和你的模式的 regex101 演示,我来看看。 (是的,我对你提出的问题提出了更高的要求;你不应该期望定期得到这种程度的照顾。)
  • 对于 twitch 剪辑,它看起来像这样 clips.twitch.tv/embed?clip='CLIPNAMEHERE' 这就是我想要的,我不希望你花费比你已经拥有的更多的时间,老实说,你是这个网站上的上帝哈哈
  • 刚刚意识到我的尝试是错误的,因为原始链接应该是 clips.twitch.tv/'CLIPNAMEHERE''~(?:&lt;a.*?&lt;/a&gt;|&lt;img.*?&lt;/img&gt;|&lt;iframe.*?&lt;/iframe&gt;)(*SKIP)(*FAIL)|(?:\bhttps?.*?(?:[/?&amp;](?:e|vi?|ci)(?:[/=]|%3D)|clips\.twitch\.tv/[^/]+#p/(?:[^/]+/)+)([\w-]))\S*~i', 转换后它转到 /embed?clip=
  • 很好,那么你就正确地使用了 StackOverflow。我很乐意随时为您提供帮助。如果您对我的正则表达式模式有什么特别不明白的地方,(在研究后)欢迎您来问我。
【解决方案2】:

我设法通过交换我的代码解决了我的问题,因此它首先转换 YouTube 链接,然后是其他 URL,然后稍作更改。新代码:

$youtubeURL = '/\s*[a-zA-Z\/\/:\.]*youtube.com\/watch\?v=([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i';
$string = preg_replace($youtubeURL, '<br><iframe width="640" height="360" src="https://www.youtube.com/embed/$1$3" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe><br>', $string);
$url = '~(\s|^)(https?://.+?)(\s|$)~im';
$string = preg_replace($url, '<a href="$0">$0</a>', $string);

【讨论】:

    猜你喜欢
    • 2020-10-19
    • 1970-01-01
    • 2013-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-03
    相关资源
    最近更新 更多