Vanilla 1 is no longer supported or maintained. If you need a copy, you can get it here.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.
Options

Need a preg_replace ninja!

edited October 2006 in Vanilla 1.0 Help
I have this tag:

<mp3 src="myfile.mp3" title="my title" />

And I need it to be be automagically transformed into something like this:

<object><param name="movie" value="player.swf?src=myfile.mp3&title=my-title"></param><embed src="player.swf?src=myfile.mp3&title=my-title" type="application/x-shockwave-flash"></embed></object>

I have looked at some of the YouTube expressions SirNot and Bergmot did, but I can't get them to work for this example.

Thanks for your time.

Comments

  • Options
    bump. sorry.
  • Options
    This is pretty much what you're after - you'll just have to trawl around and find the meat of it (preg_replace stuff at the bottom)

    http://1976design.com/blog/archive/site-news/2004/07/29/redesign-tag-transform/

    HTH

    Martin.
  • Options
    $String = preg_replace('!<mp3 src="(.*?)" title="(.*?)" />!ie', '<object><param name="movie" value="player.swf?src=$1&title=$2"></param><embed src="player.swf?src=$1&title=$2" type="application/x-shockwave-flash"></embed></object>', $String);

    I came up with that, but, I have no idea how to make it into an extension now to test it.

    Ideally, I would like it to work regardless of the post format (text, html, bbcode, etc.).

    Can anyone help with that now?

    sweeny, that really helped!
  • Options
    If you plan on the attributes being in that order, then you could something along the lines of:
    $out = preg_replace_callback(
    	'/<mp3\s+src\s*=\s*(["\']?)(.+?)\\1\s+title\s*=\s*(["\']?)(.+?)\\3[^>]*>/si', 
    	create_function(
    		'$m', 
    		'$url = \'player.swf?src=\'.urlencode($m[2]).\'&title=\'.urlencode($m[4]);'.
    			'return \'<object><param name="movie" value="\'.$url.\'" />'.
    			'<embed src="player.swf?\'.$url.\'" '.
    			'type="application/x-shockwave-flash"></embed></object>\';'
    	), 
    	$text
    );
    But if the order didn't matter then you'd want to look through the inside of the tag itself.
  • Options
    They would be in that order, yes, but I have no idea how to make that into an extension. Could you show me how to have it work for any format the comment is on, not just text, or not just html. etc.
  • Options
    class Mp3Replace extends StringFormatter
    {
    	function Parse($String, $Object, $FormatPurpose)
    	{
    		if($FormatPurpose == FORMAT_STRING_FOR_DISPLAY)
    		{
    			return preg_replace_callback(
    					'/<mp3\s+src\s*=\s*(["\']?)(.+?)\\1\s+title\s*=\s*(["\']?)(.+?)\\3[^>]*>/si', 
    					create_function(
    						'$m', 
    						'$url = \'player.swf?src=\'.urlencode($m[2]).\'&title=\'.urlencode($m[4]);'.
    							'return \'<object><param name="movie" value="\'.$url.\'" />'.
    							'<embed src="player.swf?\'.$url.\'" '.
    							'type="application/x-shockwave-flash"></embed></object>\';'
    					), 
    					$String
    				);
    		}
    		else return $String;
    	}
    }
    
    $Mp3Replace = $Context->ObjectFactory->NewContextObject($Context, 'Mp3Replace');
    $Context->StringManipulator->AddGlobalManipulator('Mp3Replace', $Mp3Replace);
    or something to that effect...
This discussion has been closed.