Extract src from google maps embed link using regex - php

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

Related

open in new tab using php

I want to open the maps in new tap when user click on "view larger map"
'gmap'=>array(
array(
'(https*:\/\/maps.google.co.in\/?[^< ]+)',
'<iframe width="'.qa_opt('embed_gmap_width').'" height="'.qa_opt('embed_gmap_height').'" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="$1&ie=UTF8&output=embed"></iframe><br /><small>View Larger Map</small>','gmap'
)
),
);
where should I put target="_blank
You should put target="_blank" inside <a> tag. Reference
View Larger Map
Preferably in the <a> tag:
'<iframe width="'.qa_opt('embed_gmap_width').'" height="'.qa_opt('embed_gmap_height').'" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="$1&ie=UTF8&output=embed"></iframe><br /><small>View Larger Map</small>',

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.

PHP Coding making different Iframe codes appear

Hello so I have this code here <iframe frameborder="0" height="550" marginheight="0" marginwidth="0" scrolling="auto" src="http://example.com/signup.php" width="1000"></iframe> now I was wondering is there any way to make a php script that can choose a random iframe code and display the php script on the page for example
<iframe frameborder="0" height="550" marginheight="0" marginwidth="0" scrolling="auto" src="website1.com/signup.php" width="1000"></iframe>
<iframe frameborder="0" height="550" marginheight="0" marginwidth="0" scrolling="auto" src="website2.com/signup.php" width="1000"></iframe>
like in website 1 and 2 but have it so each time a person loads that page it switches from site 1 to site 2 and embeds the php or is that n
You could simplify this easily. Try adding the sites to an array like this:
$sites = array(
'site1.com',
'site2.com',
'site3.com',
'site4.com',
'site5.com',
'site6.com',
'site7.com'
);
Now you can use features like array_rand() to pick a random entry from the site to show in your <iframe>:
<?php
$site_to_use = array_rand($sites);
?>
<iframe frameborder="0" height="550" marginheight="0" marginwidth="0" scrolling="auto" src="<?php echo $sites[$site_to_use]; ?>/signup.php" width="1000"></iframe>
Or you could even use rand() like this:
<iframe frameborder="0" height="550" marginheight="0" marginwidth="0" scrolling="auto" src="<?php echo $sites[rand(0, (count($sites - 1)))]; ?>/signup.php" width="1000"></iframe>
That avoids the hassles of large if else or switch casestatements.
Example

Embedd HTML code from PHP variable

After more than 3 hours searching in google, I found that ppl do the other way around of what I am doing but anyway, here is what I want to do:
I have a variable $location which contains an HTML code (embedded google map code) i.e.
<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0"
marginwidth="0" src="https://www.google.com/maps/ms?........in a larger
map></iframe>
now in a php code, how can i get the map to be displayed?
the following outputs the HTML code as text
<?php echo $loation; ?>
Thanks!
You spelled your variable name wrong $loation; - should be $location;. Assuming you set your variable correctly like
$location = '...';
OR using heredoc syntax:
$location = <<<STR
<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0"
marginwidth="0" src="https://www.google.com/maps/ms?........in a larger
map></iframe>
STR;
you should be fine.

Replace value of string in php?

I have this string:
$string=<iframe width="425" height="350" frameborder="0" scrolling="no"
marginheight="0" marginwidth="0" src="www.yourwebiste.com"></iframe>
I want replace width from 425 to 248 and height 350 to 160?
I have tried with
str_replace('"350"', '"160"', $string);
But i got no luck
As meustrus mentioned, first of all you need to put quotes around $string.
To replace the values you can use a single str_replace:
$string='<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="www.yourwebiste.com"></iframe>';
$newString = str_replace(array('425', '350'), array('248', '160'), $string);

Categories