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;
Related
I have a submit form for adding videos on my wordpress site that I create this code...
<?php
$url1 = get_post_meta( $post->ID , 'video_play' , true );
$search = '#(.*?)(?:href="https?://)?(?:www\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|/watch?.*?v=))([\w\-]{10,12}).*#x';
$replace = 'http://www.youtube.com/embed/$2';
$url = preg_replace($search,$replace,$url1);
?>
<iframe width="560" height="315" src="<?php echo $url; ?>" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
This code will take youtube url and change it to embed code and add src to iframe, also i can post src source from embedable source and it will also work, but...
What I want to do now is if in field I put
<iframe width="560" height="315" src="https://www.youtube.com/embed/videoid" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
take https://www.youtube.com/embed/videoidfrom code above and put it in $url.
When I try it with existing code, my output is...
<iframe width="560" height="315" src="<iframe width=" iframe-embed"="" scrolling="no" frameborder="0" allowfullscreen="true" webkitallowfullscreen="true" mozallowfullscreen="true"></iframe>
Update:
I found the way how embed codes will be extracted its via this function...
<?php
$url1 = get_post_meta( $post->ID , 'video_play' , true );
$search = '#(.*?)(?:href="https?://)?(?:www\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|/watch?.*?v=))([\w\-]{10,12}).*#x';
$replace = 'http://www.youtube.com/embed/$2';
$url2 = preg_replace($search,$replace,$url1);
$url3 = preg_match('~iframe.*src="([^"]*)"~', $url2, $result);
$url4 = $result[1];
?>
<iframe width="560" height="315" src="<?php echo $url4; ?>" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
Now, I need to combine first function and seconds one, php code should check if its embed code that has SRC value to use preg match for src andf extract src, else it should use first function.
Something like check if SRC value exists, and than use...
$url3 = preg_match('~iframe.*src="([^"]*)"~', $url2, $result);
$url4 = $result[1];
otherwise...
$url4 = $url2;
<?php
$url1 = get_post_meta( $post->ID , 'video_play' , true );
$search = '#(.*?)(?:href="https?://)?(?:www\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|/watch?.*?v=))([\w\-]{10,12}).*#x';
$replace = 'http://www.youtube.com/embed/$2';
$url2 = preg_replace($search,$replace,$url1);
$url3 = '<iframe width="560" height="315" src="'.$url2.'" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>';
$url4 = preg_match('~iframe.*src="([^"]*)"~', $url3, $result);
$url4 = $result[1];
?>
<iframe width="560" height="315" src="<?php echo $url4; ?>" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
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
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
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 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);