Replace value of string in php? - 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);

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

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 preg_replace Replace specific string with specific valus

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;

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