Scriptplayground Network

Scriptplayground Preview
Fireworks CS4 Learning Center

Flash and PHP Bible

The Flash and PHP Bible has been released! The book can be found on Amazon or wherever fine books are sold in your area. This book explains the process of working with PHP in Flash, while creating real world examples that you can actually learn something from.

The Flash and PHP Bible has a dedicated forum for support and comments.

Scriptplayground » tutorials » php » Remove OBJECT code using PHP

Remove OBJECT code using PHP

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.

<object
This 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.

| Print It |  Follow Scriptplayground on Twitter (@scriptplay)

Comments: Remove OBJECT code using PHP

 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
Add a comment
Name:
Website:
Comment:
Please note: Offensive comments, flaming and spamming is not permitted on this site and your comment will be deleted immediately.

HTML is not allowed.

Please provide all comments in English so that others can help you. A common helper in this is to use an online translator.

As a security measure your ip will be recorded.
 
Anti-Robot Check:

Enter the key you see above.

What is this?: This extra test has been added due to the recent explosion of spam.
 
Google