Specifically convert only YouTube urls to iframe t

2019-02-27 19:20发布

I'm trying to convert youtube video links into iframes to embed them onto my website but I'm having a problem as I am already converting urls to anchor tags and it's conflicting with the youtube url.

Here is my code:

$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);

The first line gets anything that is just a plain url as I am using BBCode it conflicted with other stuff like the [img] tag. The $youtubeURL is to get all youtube links and turn them into iframes but because of the first link conversion it causes an href to be put in the scr=.

2条回答
The star\"
2楼-- · 2019-02-27 19:32

I managed to fix my problem by swapping my code around so it converts YouTube links first then the other URLs second and changing it a little. New code:

$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);
查看更多
淡お忘
3楼-- · 2019-02-27 19:41

Launching from your previous question, I've developed and tested a complete battery of regular expressions to get you on your way. I will admit that this set of pattern could be refined further to be more condensed (fewere total patterns) and that the youtube validation patterns could be more strict, but I honestly don't have time to go down those rabbit holes.

When considering the compexity/diversity of youtube urls, you may want to reference this list of examples that I pulled from StackOverflow pages and the internet. https://regex101.com/r/zinjze/1

Code: (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);

Unrendered Output: (you can copy-paste my snippet in http://phptester.net/ if you want to see the rendered output)

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>
查看更多
登录 后发表回答