replace a string in a youtube embed code using jquery - php

I have here an embed code from youtube and Im using a plugin to show that video
<object width="350" height="200">
<param name="allowfullscreen" value="true" />
<param name="allowscriptaccess" value="always" />
<param name="movie" value="http://www.youtube.com/v/KVu3gS7iJu4&autoplay=0&loop=0&rel=0" />
<param name="wmode" value="transparent">
<embed src="http://www.youtube.com/v/KVu3gS7iJu4&autoplay=0&loop=0&rel=0" type="application/x-shockwave-flash" wmode="transparent" allowfullscreen="true" allowscriptaccess="always" width="350" height="200">
</embed>
</object>
My problem is I cannot edit those codes, I need to change the "&" sign to "&" for w3c validation, is this possible using jquery
here is My sample fiddle which is not working and dont know how http://jsfiddle.net/kfX9M/

Changing it with jQuery won't make your HTML valid. You could change it server-side with some PHP:
$dom = new DOMDocument();
$dom->loadHTML($your_embed_code_here);
$xpath = new DOMXPath($dom);
$items = $xpath->query("//*[contains(#href,'&')]");
$l = $item->length;
for($i=0; $i<$l; $i++) {
$items->item($i)->setAttribute("href",str_replace("&","&",$items->item($i)->getAttribute("href")));
}
$out = $dom->saveHTML();
But, as mentioned by elclanrs, it really makes no difference.

Hi dude i did with jQuery and Regular Expressions.,
<script type="text/javascript">
$(document).ready( function() {
var re=/&/g;
var value=$("param[name='movie']").val();
var modified_value=value.replace(re,'&');
$("param[name='movie']").val(modified_value);
var src = $("embed").attr('src');
var src_modified_value=src.replace(re,'&');
$("embed").attr('src',src_modified_value);
});
</script>
i think this may help you to resolve your problem.
To see it in action : http://jsfiddle.net/john_rock/7H2Hk/

Related

Flash Banner and PHP

For some reason I'm trying to do:
<OBJECT id="affMV737" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" width="468" height="60">
<param name="movie" value="http://www.domain.com/view.php?banner_id=1">
<param name="allowScriptAccess" value="always">
<embed src="http://www.domain.com/view.php?banner_id=1" width="468" height="60" allowScriptAccess="always" ALIGN="" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></embed>
</object>
I'm printing the FULL URL in the php but the flash is not loading...
but If i change the src in the flash code to the printed result in the php, the banner is working.
any advice?
thanks.
Not clear what you want but I guess this will help
instead of this:
http://www.domain.com/view.php?banner_id=1
try like this:
<?php echo file_get_contents('http://www.domain.com/view.php?banner_id=1');?>
update:
Another guess.. You need change your view.php to something like this:
header('Content-type: application/x-shockwave-flash');
echo file_get_contents('http://domain.com/files/bnrs/blabla.swf');

php preg_replace width and height inside of object?

hey guys,
a php variable $htmlholds the following object...
<object width="562" height="200">
<param name="movie" value="http://www.youtube.com/v/rBa5qp9sUOY?version=3">
<param name="allowFullScreen" value="true">
<param name="allowscriptaccess" value="always">
<embed src="http://www.youtube.com/v/rBa5qp9sUOY?version=3"
type="application/x-shockwave-flash"
width="562"
height="200"
allowscriptaccess="always"
allowfullscreen="true">
</object>
any idea how I could filter the width and height both of the object- and embed-tag to have 100% values?
<object width="100%" height="100%">
...
<embed src="http://www.youtube.com/v/rBa5qp9sUOY?version=3"
type="application/x-shockwave-flash"
width="100%"
height="100%"
allowscriptaccess="always"
allowfullscreen="true">
</object>`
Thanks for your help!
update:
$chunk = $dom->getElementsByTagName('body')->item(0);
$objectHtml = '';
foreach($chunk->childNodes as $node) {
$objectHtml .= $node->saveXML();
}
return $objectHtml;
says: Call to undefined method DOMElement::saveXML()
whereas...
$dom = preg_replace('/^<!DOCTYPE.+?>/', '', str_replace( array('<html>', '</html>', '<body>', '</body>'), array('', '', '', ''), $dom->saveXML()));
works fine! however I get weird inserted in my sourcecode when using this..
like this:
<!--?xml version="1.0" standalone="yes"?-->
<br>
<br>
<object width="95%" height="75%"><param name="movie" value="http://www.youtube.com/v/rBa5qp9sUOY?version=3"><param name="allowFullScreen" value="true"><param name="allowscriptaccess" value="always"><embed src="http://www.youtube.com/v/rBa5qp9sUOY?version=3" type="application/x-shockwave-flash" width="95%" height="75%" allowscriptaccess="always" allowfullscreen="true"></object>
any idea what I'm doing wrong here?
It is often better to use a HTML parser instead of a regex when modifying HTML.
$dom = new DOMDocument;
$dom->loadHTML($str);
$object = $dom->getElementsByTagName('object')->item(0);
$object->setAttribute('width', '100%');
$object->setAttribute('height', '100%');
$embed = $object->getElementsByTagName('embed')->item(0);
$embed->setAttribute('width', '100%');
$embed->setAttribute('height', '100%');
echo $dom->saveXML($dom->getElementsByTagName('body')->item(0));
You probably don't want the element wrapped in body, so do this...
$objectHtml = '';
foreach($chunk->childNodes as $node) {
$objectHtml .= $dom->saveXML($node, LIBXML_NOEMPTYTAG);
}
Where $chunk is $dom->getElementsByTagName('body')->item(0).
I added the option LIBXML_NOEMPTYTAG, otherwise if you have <span></span> it will be turned into <span />.
If using >= PHP 5.3, use saveHTML() instead.
Is that all that your string will contain? If so, you can use this:
$replace = preg_replace(
"/(width|height)=\".*?\"/",
"\${1}=\"100%\"",
$html
);
You can tweak the pattern to allow for whitespace between the attribute, = sign, and value as necessary.
If your string will contain more HTML, with their own width/height attributes you don't want replaced, then you'll need a more robust solution.
preg_replace('/(width|height)(=)"([\d]+)"/', '${1}${2}"100%"', $html);
That should do the trick :)
$html = '<object width="562" height="200"><param name="movie" value="http://www.youtube.com/v/rBa5qp9sUOY?version=3"><param name="allowFullScreen" value="true"><param name="allowscriptaccess" value="always"><embed src="http://www.youtube.com/v/rBa5qp9sUOY?version=3" type="application/x-shockwave-flash" width="562" height="200" allowscriptaccess="always" allowfullscreen="true"></object>';
$pattern = '/(<object[^>]*width=)\".*?(\"[^>]*height=)\".*?(\"[^>]*>.*?<embed[^>]*width=)\".*?(\"[^>]*height=)\".*?(\"[^>]*>.*?<\/object>)/si';
$replace = '\1"100\2"200\3"300\4"400\5';
echo preg_replace($pattern, $replace, $html);
This should work.
Set, 100, 200, 300, 400 to your values.

Finding string between custom tags and doing something with it

On my new project, members are able to use my YouTube API and it will generate a tag like below for them to use it in whatever textarea they want:
<ymedia>http://www.youtube-nocookie.com/v/qQG0XfU-bFs</ymedia>
I want my PHP profile page to take the string between <ymedia></ymedia> and generate a similar code below using $ymstring for the string collected from the ymedia tag:
<?php
$u_agent = $_SERVER[''HTTP_USER_AGENT''];
if(preg_match(''/Opera/i'',$u_agent)){
<object width="320" height="240" data="".$ymstring."?fs=1&hl=en_US&rel=0"><param name="movie" value="".$ymstring."?fs=1&hl=en_US&rel=0" /><param name="allowFullScreen" value="true" /><param name="wmode" value="transparent" /><param name="bgColor" value="#ffffff" /><param name="flashvars" value="vu=".$ymstring."?fs=1&hl=en_US&rel=0" /></object>
} else {
<object width="320" height="240"><param name="movie" value="".$ymstring."?fs=1&hl=en_US&rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="".$ymstring."?fs=1&hl=en_US&rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="320" height="240"></embed></object>
}
?>
Basically, I'm trying to find all the ymedia tags in a $string and replace them with my youtube coding above aimed at either Opera or the rest of other browsers.
This should work:
    preg_match("/<ymedia>(.*?)<\/ymedia>/", $Input, $Matches);
Then, your result would be in $Matches[1] which you could pass as $ymstring.
You want to use preg_replace_callback for simplicity:
$html = preg_replace_callback('#<ymedia>(http://.*?)</ymedia>#',
"ymedia_cb", $html);
function ymedia_cb($matches) {
$url = $matches[1];
return "<object src=$url>";
}
Obviously you would use your if/else construct to return the appropriate and complete object tag.

How to pass data with '&' between web pages

The jQuery -
$.ajax({
type: "GET",
url: "add_showcase.php",
data: "title=" + document.getElementById("title_in").value + "&desc=" + document.getElementById("desc_in").value,
success: function(html)....
When i pass this data as "desc" -
<object width="480" height="385">
<param name="movie" value="http://www.youtube.com/v/7M-jsjLB20Y?fs=1&hl=en_US"></param>
<param name="allowFullScreen" value="true"></param>
<param name="allowscriptaccess" value="always"></param>
<embed src="http://www.youtube.com/v/7M-jsjLB20Y?fs=1&hl=en_US" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed>
</object>
add_showcase.php only receives a part of this data -
desc=<object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/7M-jsjLB20Y?fs=1
What would you do if you were to rectify this problem?
Replace the & with the hex value %26 so it looks like http://www.youtube.com/v/7M-jsjLB20Y?fs=1%26hl=en_US
To do this, use Javascript's escape() function. If you need to, use Javascript's unescape() function to convert the hex values back to their original values.
Use POST instead of GET if you have huge data
use
escape(document.getElementById("desc_in").value)

Hyperlink Youtube to Embed Code

I've some troubles using Preg_replace and preg_match_all to convert a Youtube URL to embed code. Yes, I know that this topic has already touched in stackoverflow but not exactly like I want.
I can get the ID from a url, without html, with that:
http://(?:www\.)?youtu(?:be\.com/watch\?v=|\.be/)(\w*)(&(amp;)?[\w\?=]*)?
But I've the url formatted with like this:
http://www.youtube.com/watch?v=C9KAqhbIZ7o
And I want to convert it all to this:
<object width="640" height="505"><param name="movie" value="http://www.youtube.com/v/C9KAqhbIZ7o?fs=1&hl=es_ES&rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/C9KAqhbIZ7o?fs=1&hl=es_ES&rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="505"></embed></object>
Somebody can do some magic and tell me the correct expression to detect all the url, get the ID once and convert all to an embed code? Thank you so much in advance!
Update information:
In order to help and make it a more concise...
I've this:
<p>This is an example of comment</p><strong>Hi bold!</strong><p>Look a youtube url! http://www.youtube.com/watch?v=C9KAqhbIZ7o</p>
And I want to get this:
<p>This is an example of comment</p><strong>Hi bold!</strong><p>Look a youtube url! <object width="640" height="505"><param name="movie" value="http://www.youtube.com/v/C9KAqhbIZ7o?fs=1&hl=es_ES&rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/C9KAqhbIZ7o?fs=1&hl=es_ES&rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="505"></embed></object></p>
Thanks all for your help, I really appreciate it!
I use this code
// url of video
$url = $row['url'];
$id=0;
// we get the unique video id from the url by matching the pattern
preg_match("/v=([^&]+)/i", $url, $matches);
if(isset($matches[1])) $id = $matches[1];
if(!$id) {
$matches = explode('/', $url);
$id = $matches[count($matches)-1];
}
// this is your template for generating embed codes
$code = '<div id="img_wrapper"><object width="640" height="458"><param name="movie" value="http://www.youtube.com/v/{id}&hl=en_US&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/{id}&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object></div>';
// we replace each {id} with the actual ID of the video to get embed code for this particular video
$code = str_replace('{id}', $id, $code);
echo $code;
Replace $html with your html string which needs parsing.
$html=<<<HTML
<p>This is an example of comment</p><strong>Hi bold!</strong><p>Look a youtube url! http://www.youtube.com/watch?v=C9KAqhbIZ7o</p>
HTML;
$regex="/v\=([\-\w]+)/";
preg_match_all($regex,$html,$out);
$out[1]=array_unique($out[1]);
foreach($out[1] as $o){
$reg="/(<a).*(youtube.com).*($o).*(\/a>)/";
$embed= <<<HTML
<object width="640" height="505"><param name="movie" value="http://www.youtube.com/v/$o=1&hl=es_ES&rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/$o?fs=1&hl=es_ES&rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="505"></embed></object>
HTML;
$html=preg_replace($reg,$embed, $html);
}
echo $html;

Categories