The Flash and PHP Bible has been released! The book can be found on Amazon or wherever fine books are sold in your area.
The Flash and PHP Bible has a forum for quick support.
Using PHP to remove OBJECT code and replace it. This article uses preg_replace and a regular expression to achieve this.
View an Example of this article before you get started.
Someone had asked me if it was possible to strip out object code (usually seen in flash enabled sites) using PHP. I told him it is possible and a regular expression would be needed. He thenasked if I could write up an example because he was a novice in regex, so I did. Now I am publishing it for everyone to get use of. :)
$object_code = " <object classid=\"clsid: D27CDB6E-AE6D-11cf-96B8-444553540000\"
codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0\"
width=\"502\" height=\"418\">
<param name=\"movie\" value=\"fs_light.swf\">
<param name=\"quality\" value=\"high\">
<param name=\"wmode\" value=\"transparent\">
<embed src=\"fs_light.swf?uri=*sta\" menu=\"false\" quality=\"high\"
pluginspage=\"http://www.macromedia.com/go/getflashplayer\"
type=\"application/x-shockwave-flash\"
width=\"502\" height=\"418\"></embed>
</object>";
The sample object code I used to test with, this is just your basic object code found on Flash enabled sites.
It is written in a non-formatted manner specifically to test the worse case scenario.
preg_replace("/<object[0-9 a-z_?*=\":\-\/\.#\,<>\\n\\r\\t]+<\/object>/smi",
"[OBJECT_CODE_WAS_HERE]", $object_code);
The PHP to achieve this, we use preg_replace which is a regular expression recursive pattern match.
The regular expression may look advanced but in fact it is just searching for a series of characters. I will quickly explain what is happening in the regular expression, for a more in depth article I recommend this one.
<objectThis is the starting code for the object tag.
[0-9 a-z_?*=\":\-\/\.#\,<>\\n\\r\\t]
Before we get started it is good to note that '\' is used to escape characters that are used in a regular expression such as '.'
The "[" is the start of a character class.
0-9 a-z is used to match all alphanumeric characters and spaces.
Then we test for other characters _?*=\":\-\/\.#\,<>\\n\\r\\t, these characters include dashes, underscores, equals. The other characters we search for are newlines '\n' or '\r' and tabs '\t.
The "]" is the end of a character class.
That is the end of the regular expression. Next we replace the result with "[OBJECT_CODE_WAS_HERE]", you could also replace it with blank to completely remove the match.
Then we pass the $object_code variable into the preg_replace to search it.
That is all it takes to search and replace OBJECT code, for your convenience I have included the code I used to test this.
<?php
$object_code = " <object classid=\"clsid: D27CDB6E-AE6D-11cf-96B8-444553540000\"
codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0\"
width=\"502\" height=\"418\">
<param name=\"movie\" value=\"fs_light.swf\">
<param name=\"quality\" value=\"high\">
<param name=\"wmode\" value=\"transparent\">
<embed src=\"fs_light.swf?uri=*sta\" menu=\"false\" quality=\"high\"
pluginspage=\"http://www.macromedia.com/go/getflashplayer\"
type=\"application/x-shockwave-flash\"
width=\"502\" height=\"418\"></embed>
</object>";
$object_code_stripped = preg_replace("/<object[0-9 a-z_?*=\":\-\/\.#\,<>\\n\\r\\t]+<\/object>/smi",
"[OBJECT_CODE_WAS_HERE]", $object_code);
?>
<strong>Un-modified Code</strong>
<pre>
<?php print htmlspecialchars($object_code); ?>
</pre>
<strong>Modified Code</strong>
<pre>
<?php print htmlspecialchars($object_code_stripped); ?>
</pre>
That is the end of this article, now you should have an understanding of how to remove OBJECT code from a string. Until next time, happy programming.
|
Guest Wed Aug 1, 2007 6:44 pm
nice!!!! exactly what i was looking for
|
|
Guest Wed Nov 19, 2008 5:30 pm
Thanks..for the tutorial
|
|
Drake Wed Nov 18, 2009 3:49 am
HI,
I am looking for something to replace the width and height in the object tag i tried using regex but my skills are still under development. so if you could i would appreciate some help on archiving this. cartman.wayne3@gmail.com |
|
Dan Sargeant Sat Jan 30, 2010 1:41 am
You have missed out the semicolon from your list of allowed characters. If anyone has used the style attribute on the object tag this regex will not work as styles will be seperated by semi colons
|
|
mkeefe Sat Jan 30, 2010 10:57 am
@Dan - While I agree the semicolon ";" is not included, why would you need to style the OBJECT tag? Its generally used to include some other asset such as FLASH, VIDEO, etc...
|
|
Henri Sat Feb 6, 2010 7:08 am
That's nice. But there seems to be a problem: try with an input containing more than one object tags.
|
|
mkeefe Thu Feb 25, 2010 6:34 pm
@Henri - Correct, it will only work with OBJECT tag. Simple re-run it and you should be all set.
|
©2004 - 2010 scriptplayground | Privacy Policy | Legal
Validate Site: XHTML CSS | Designed by: Matthew Keefe of mkeefeDESIGN