Php 在将BBCode URL转换为html元素时,专门将YouTube URL转换为iframe标记

Php 在将BBCode URL转换为html元素时,专门将YouTube URL转换为iframe标记,php,regex,youtube,preg-replace,bbcode,Php,Regex,Youtube,Preg Replace,Bbcode,我正在尝试将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, '[

我正在尝试将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);
$string='1!'https://www.google.com 及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,,$string);

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

中。我设法解决了我的问题,方法是交换代码,使其先转换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);
$youtubeURL='/\s*[a-zA-Z\/\:\.]*youtube.com\/watch\?v=([a-zA-Z0-9\-\\+)([a-zA-Z0-9\/\*-\\\\?&\%\=.]*)/i';
$string=preg_replace($youtubeURL,

,$string); $url='~(\s |^)(https?:/。+?)(\s |$)~im'; $string=preg_replace($url,,$string);
从开始,我开发并测试了一套完整的正则表达式,帮助您顺利完成任务。我承认,这组模式可以进一步细化为更精简的模式(fewere total模式),youtube验证模式可以更严格,但老实说,我没有时间钻这些空子

在考虑youtube URL的竞争性/多样性时,您可能希望参考我从StackOverflow页面和internet中提取的示例列表

代码:()

未渲染输出:(如果要查看渲染输出,可以复制粘贴我的代码段)


志愿者们,您可能会发现查看此OP的上一个问题有助于了解有关该问题的更多上下文。这可能很有价值:因此基本上,对于每个您不希望与BBCode标记中包含的url匹配的问题?我希望能够仅将youtube链接转换为iFrame,因此,那些不包含任何BBCode或像google.com这样的普通链接的用户可以使用这些
$url='~(?>\[^\[\]]*\](*跳过)(*失败)\s*)*\s*\Khttps?://.+?(?!\s)~
$youtubeURL='~(?>\[^\[\]]*\](*跳过)(*失败)\s*)*\s*[a-zA-Z/:.]*youtu(be\.com/watch\?v=\\.be/)([a-zA-Z0-9\-+)([a-zA-Z0-9/*.&%=.-])“~(?:很好,那么你正确地使用了StackOverflow。我很乐意随时帮助你。如果我的正则表达式模式有什么特别的地方你不明白,(在研究之后)欢迎你问我。
$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);
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>