How can I replace a soundcloud url to an iFrame (PHP) - php

How can I detect and replace a soundcloud url in a text with an iframe using PHP:
For example:
This:
https://soundcloud.com/s/eminem-ft-dr-dre-old-time-sake
Into this:
<iframe width="100%" height="166" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/140068709&color=00aabb&auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false"></iframe>

function linkifySoundcloudURLs( $text )
{
$text = preg_replace('#https{0,1}:\/\/w{0,3}\.*soundcloud\.com\/([^< ]+)#ix',
'<iframe width="100%" height="166" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https://soundcloud.com/$1&color=00aabb&auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false"></iframe>',
$text);
return $text;
}
Works for me.

I'm not sure I understand you correctly, but something like this?
<?php
$url = "https://soundcloud.com/aathmigan/eminem-ft-dr-dre-old-time-sake";
?>
<iframe width="100%" height="166" scrolling="no" frameborder="no"
src="<?php echo $url; ?>"></iframe>

Related

HTML string append/replace in PHP

I have the following html string in my php code:
<iframe src="https://player.vimeo.com/video/123456789" width="604" height="340" frameborder="0" title="Ovarian Psycos - Teaser" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen=""></iframe>
I want to append parameters to the URL give in the src attribute. The result should look as follows:
<iframe src="https://player.vimeo.com/video/123456789/APPENDAGE" width="604" height="340" frameborder="0" title="Ovarian Psycos - Teaser" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen=""></iframe>
As you can see, in my example the string /APPENDAGE should be appended to the URL givein in the src attribute.
Usually I would do something like this with a string-replace method, however, in the given case this approach won't work. What would be an efficient approach to reach my goal?
If you don't mind using regex, there is a simple solution for this: (DEMO)
$str = '<iframe src="https://player.vimeo.com/video/123456789" width="604" height="340" frameborder="0" title="Ovarian Psycos - Teaser" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen=""></iframe>
';
$replaced = preg_replace('/(?<=src=")(.*?)(?=")/', '$1/APPENDAGE', $str);
var_dump($replaced);
Output:
string '<iframe src="https://player.vimeo.com/video/123456789/APPENDAGE" width="604" height="340" frameborder="0" title="Ovarian Psycos - Teaser" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen=""></iframe>
' (length=215)
This will simply match the url, insdie src attr and append /APPENDAGE to it, you can change it to whatever you want.
if you don't mind a large code
<?php
$str = '<iframe src="https://player.vimeo.com/video/123456789" width="604" height="340" frameborder="0" title="Ovarian Psycos - Teaser" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen=""></iframe>';
$obj = simplexml_load_string($str);
//print_r($obj);
$obj['src'] = $obj['src'].'/APPENDAGE';
$s = $obj->asXML();
print(substr( $s, strpos($s, "\n")+1 ));
But this code will also work for rest of the attributes

Strip a section of an embed code with PHP

I'm trying to search JW Player embed codes like this one:
<iframe src="//content.jwplatform.com/players/ewQYJ6zA-F6KYzWLn.html" width="480" height="270" frameborder="0" scrolling="auto" allowfullscreen></iframe>
What I need to grab is the code right after players/ which in this embed would be just ewQYJ6zA.
Anyone have an idea how to go about this?
This will search for players/ followed by one or more letters and numbers.
<?php
$string = '<iframe src="//content.jwplatform.com/players/ewQYJ6zA-F6KYzWLn.html" width="480" height="270" frameborder="0" scrolling="auto" allowfullscreen></iframe>';
preg_match( '~/players/([[a-zA-Z\d]+)~', $string, $matches );
echo $matches[1];
?>
Output:
ewQYJ6zA
This regex in PHP should do the trick:
preg_match('/players\/(.+)-/im', $codetomatch, $codeyouwant);
Regex101

How to get all YouTube iframe from HTML using regex

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.

Get youtube/vimeo id with pregmatch

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];

Extract src from google maps embed link using regex

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

Categories