Using PHP to remove OBJECT code and replace it. This article uses preg_replace and a regular expression to achieve this.
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.