I am currently looking at a piece of code for a friend and I am trying to strip out a piece of unwanted code but not sure how I can achieve what I want.
CODE:
<?php
$blah = '<iframe src="//player.vimeo.com/video/82444237" width="500" height="281"
frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
<p>KING OF THE BEASTS from
John Wiley on
Vimeo.</p>';
echo($blah);
?>
In this code the website is showing: from on
I want the start point of the input to be iframe and the endpoint to be /iframe.
Without manually making sure the iframe only is selected, any suggestions on how to achieve this?
Try this:
function blah($postTag)
{
//TERMINATES THE STRING AT </IFRAME>
$exploder = explode("iframe",$postTag);
//CALLS THE STRING UNTIL THE FIRST IFRAME INPUT AND CLOSES THE IFRAME TAG
$cleaned = "<iframe".$exploder[1]."iframe>";
return $cleaned;
}
$blahblah = blah('<iframe src="//player.vimeo.com/video/82444237" width="500" height="281" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe> <p>KING OF THE BEASTS from John Wiley on Vimeo.</p>');
That should do it.
A simple strstr() will do the job.
<?php
$blah = '<iframe src="//player.vimeo.com/video/82444237" width="500" height="281" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe> <p>KING OF THE BEASTS from John Wiley on Vimeo.</p>';
echo strstr($blah,'</iframe>',true)."</iframe>";
OUTPUT
<iframe src="//player.vimeo.com/video/82444237" width="500" height="281" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
$blah = '<iframe src="//player.vimeo.com/video/82444237" width="500" height="281" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe> <p>KING OF THE BEASTS from John Wiley on Vimeo.</p>';
echo substr($blah, 0, strpos($blah, '</iframe>')) ."</iframe>";
Output:
<iframe src="//player.vimeo.com/video/82444237" width="500" height="281" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
Related
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=.
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>',
'$2',
'<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>',
'$2',
'$1',
'<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>',
'$0'
);
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:https://www.example.com/example?ohyeah=sure#okay this is a raw link</li>
<li><em>No attibute</em> bbcode url: http://example.com/x1</li>
<li>A url with link and link text: x2</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>mickmackusa</li>
<li>Code:
<code>while ($noSuccess) {
tryAgain();
if ($dead) break;
}</code></li>
</ul>
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, '$0', $string);
I want to get all YouTube iframe using regex and want to add specific tag to each record found.
For example <youtube-frame></youtube-frame> to iframe begeing and end.
Needed output:
<youtube-frame><iframe width="560" height="315" src="https://www.youtube.com/embed/vakfMRyjulw" frameborder="0" allowfullscreen></iframe></youtube-frame>
<youtube-frame><iframe width="560" height="315" src="https://www.youtube.com/embed/aDGWMlKPKDs" frameborder="0" allowfullscreen></iframe></youtube-frame>
My Code
$embed = '
<iframe width="560" height="315" src="https://www.youtube.com/embed/vakfMRyjulw" frameborder="0" allowfullscreen></iframe>
<iframe width="600" height="350" src="https://tune.pk/player/embed_player.php?vid=6508414&folderp2016/05/04/&width=600&height=350&autoplay=no" frameborder="0" allowfullscreen scrolling="no"></iframe>
<iframe width="560" height="315" src="https://www.youtube.com/embed/aDGWMlKPKDs" frameborder="0" allowfullscreen></iframe>
<iframe width="600" height="350" src="https://tune.pk/player/embed_player.php?vid=6508414&folder=2016/05/04/&width=600&height=350&autoplay=no" frameborder="0" allowfullscreen scrolling="no"></iframe>
<iframe width="600" height="350" src="https://tune.pk/player/embed_player.php?vid=6508414&folder=2016/05/04/&width=600&height=350&autoplay=no" frameborder="0" allowfullscreen scrolling="no"></iframe>
';
What I have tried?
$pattern = '/<iframe\.*src=\"//youtube"\.*/';
$iframeSrc = preg_match($pattern, $embed, $matches);
var_dump($iframeSrc);
Try this:
$iframeSrc = preg_replace('/<iframe[^>]*src\s*=\s*"?https?:\/\/[^\s"\/]*youtube.com(?:\/[^\s"]*)?"?[^>]*>.*?<\/iframe>/i', '<youtube-frame>$0</youtube-frame>', $embed);
This uses preg_replace and a global regex to replace all YouTube IFrame tags (including their closing tags) with <youtube-frame>$0</youtube-frame> where $0 is the original string.
The regex could theoretically be simplified if you are totally sure about the format of your input, but I designed it to be robust enough to cope with other syntaxes such as src=http://example.com or src = "http://example.com" etc. which are accepted by browsers nowadays, and it only matches sources on *.youtube.com domains and not something like myyoutubesite.com.
I studied following example
$copy_date = "Copyright 1999";
$copy_date = preg_replace("([0-9]+)", "2000", $copy_date);
here any numeric will be replaced by 2000
But in following example I am confused !!
How to replace
width="anything" with value 280
Want to Substitute anything that appears after
width="
with
width="280"
example
width="481"
will be width="280"
Another example.....
<iframe width="680" height="480" src="//www.youtube.com/embed/RTcgXcz-_G0" frameborder="0" allowfullscreen></iframe>
after preg_replace
should become
<iframe width="280" height="200" src="//www.youtube.com/embed/RTcgXcz-_G0" frameborder="0" allowfullscreen></iframe>
use this:
$copy_date = '<iframe width="680" height="480" src="//www.youtube.com/embed/RTcgXcz-_G0" frameborder="0" allowfullscreen></iframe>' ;
$pattern = '/width="\d+"/i' ;
$new_style = 'width="280"' ;
$new_copy_date = preg_replace($pattern, $new_style, $copy_date) ;
echo $new_copy_date;
How can I get the video ID for youtube and vimeo videos embed code? A youtube video embed code looks like this:
<iframe width="560" height="315" src="http://www.youtube.com/embed/dbPmWbqAJPs" frameborder="0" allowfullscreen></iframe>
where the ID in this is dbPmWbqAJPs.
A vimeo embed code looks like this:
<iframe src="http://player.vimeo.com/video/62468031?color=00ff00" width="500" height="281" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
where the ID in this example is 62468031.
I don't want to replace those exact ids, but rather have a pregmatch/pregreplace that finds the id in any embed of vimeo and of youtube.
try this
vimeo
$string='<iframe src="http://player.vimeo.com/video/62468031?color=00ff00" width="500" height="281" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
preg_match_all( '~http://player.vimeo.com/video/(.*?)\?~si',$string,$M);
echo ($M[1][0]);
youtube :
$string='<iframe width="560" height="315" src="http://www.youtube.com/embed/dbPmWbqAJPs" frameborder="0" allowfullscreen></iframe>';
preg_match_all( '~http://www.youtube.com/embed/(.*?)\"~si',$string,$M);
echo ($M[1][0]);
Try below code for php, it works without pregmatch
i.e. :
http://vimeo.com/22222222
http://player.vimeo.com/video/22222222
http://vimeo.com/channels/staffpicks/22222222
http://vimeo.com/groups/groupsname/22222222
PHP Code :
$vimeoUrl = 'http://vimeo.com/channels/channelsName/22222222';
$fetchVimeoIdArr = explode('/', $vimeoUrl);
$idCounter = count($fetchVimeoIdArr) - 1;
$vimeoId = $fetchVimeoIdArr[$idCounter];
Hello everybody and thanks for reading.
I have a link like this :
<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.it/maps?f=q&source=s_q&hl=en&geocode=&q=monte+rosa&aq=&sll=45.454082,9.213138&sspn=0.009016,0.01929&t=h&gl=it&ie=UTF8&hq=&hnear=Monte+Rosa,+Province+of+Varese,+Lombardy&ll=45.690627,8.824349&spn=0.008978,0.01929&z=14&iwloc=A&output=embed"></iframe><br /><small>View Larger Map</small>
i want to extract only this
https://maps.google.it/maps?f=q&source=s_q&hl=en&geocode=&q=monte+rosa&aq=&sll=45.454082,9.213138&sspn=0.009016,0.01929&t=h&gl=it&ie=UTF8&hq=&hnear=Monte+Rosa,+Province+of+Varese,+Lombardy&ll=45.690627,8.824349&spn=0.008978,0.01929&z=14&iwloc=A&output=embed
In other words i want to extract everything after the src=" until the end of the link ".
I have been trying with the use of regex but i cant figure out the correct syntax. Some help would be most appreciated.
$html = '<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.it/maps?f=q&source=s_q&hl=en&geocode=&q=monte+rosa&aq=&sll=45.454082,9.213138&sspn=0.009016,0.01929&t=h&gl=it&ie=UTF8&hq=&hnear=Monte+Rosa,+Province+of+Varese,+Lombardy&ll=45.690627,8.824349&spn=0.008978,0.01929&z=14&iwloc=A&output=embed"></iframe><br /><small>View Larger Map</small>';
preg_match('~iframe.*src="([^"]*)"~', $html, $result);
var_dump($result[1]);