Help Regular Expressions preg_replace

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • marcionline
    New Member
    • Jun 2006
    • 2

    Help Regular Expressions preg_replace

    I have this regexp to post edonkey links that replace this

    Code:
    "#(^|(?<=[^\w\"']))(ed2k://\|file\|([^\\/\|:<>\*\?\"]+?)\|(\d+?)\|([a-f0-9]{32})\|(.*?)/?)(?![\"'])(?=([,\.]*?[\s<\[])|[,\.]*?$)#i"
    to this:

    Code:
    <a href='http://www.emugle.com/details.php?f=\\5' target='_blank'><img src='http://www.mistershare.org/home/images/misc/emugle-icon.gif' border='0'></a> <img src='http://www.mistershare.org/home/images/misc/emule.gif' border='0'> <a href='\\2'>\\3&nbsp;<b>($size)</b></a>"
    Here an example of edonkey link:

    Code:
    ed2k://|file|[Ubuntu.Linux.6.06].ubuntu-6.06-desktop-i386.iso|731744256|FB73A6DBAEFDFB7969FFD8CE8E4FC6E3|/
    This part 731744256 is the size's file in bytes.

    So, I would like know if is there a regexp to get this part of link and format to something more human (like 697.85 MB, for example) because the $size in original function only work for me when there arent't anything above the edonkey link.

    The original function is this:

    Code:
    //######## INICIO HACK EDONKEY LINKS ########
    
     $text = ' ' . $text;
    
     $size = humanize_size(preg_replace("#(^|(?<=[^\w\"']))(ed2k://\|file\|([^\\/\|:<>\*\?\"]+?)\|(\d+?)\|([a-f0-9]{32})\|(.*?)/?)(?![\"'])(?=([,\.]*?[\s<\[])|[,\.]*?$)#i", "\\4", $text));
    
     $patterns[] = "#(^|(?<=[^\w\"']))(ed2k://\|file\|([^\\/\|:<>\*\?\"]+?)\|(\d+?)\|([a-f0-9]{32})\|(.*?)/?)(?![\"'])(?=([,\.]*?[\s<\[])|[,\.]*?$)#i";
    		
     $replacements[] = "<a href='http://www.emugle.com/details.php?f=\\5' target='_blank'><img src='http://www.mistershare.org/home/images/misc/emugle-icon.gif' border='0'></a> <img src='http://www.mistershare.org/home/images/misc/emule.gif' border='0'> <a href='\\2'>\\3&nbsp;<b>($size)</b></a>";
    
     //######### FIM HACK EDONKEY LINKS ##########

    And here is the humanize_size function:
    Code:
    <?php
    
    	function humanize_size ($size, $rounder = 0, $min = '')
    {
       $sizes   = array('Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
       $rounders = array(0,   1,	2,	2,	2,	3,	3,	3,	3);
       $ext = $sizes[0];
       $rnd = $rounders[0];
    
       if ($min == 'KB' && $size < 1024)
       {
    	  $size = $size / 1024;
    	  $ext = 'KB';
    	  $rounder = 1;
       }
       else
       {
    	  for ($i=1, $cnt=count($sizes); ($i < $cnt && $size >= 1024); $i++)
    	  {
    		 $size = $size / 1024;
    		 $ext  = $sizes[$i];
    		 $rnd  = $rounders[$i];
    	  }
       }
    
       if (!$rounder)
       {
    	  $rounder = $rnd;
       }
    
       return round($size, $rounder) . '&nbsp;' . $ext;
    }		
    
    ?>
    Any help would be great.
    Last edited by marcionline; Jun 23 '06, 08:10 PM. Reason: code error
  • marcionline
    New Member
    • Jun 2006
    • 2

    #2
    UP! :) Help me please!

    Comment

    Working...