By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,968 Members | 1,691 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,968 IT Pros & Developers. It's quick & easy.

Help Regular Expressions preg_replace

P: 2
I have this regexp to post edonkey links that replace this

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

Expand|Select|Wrap|Line Numbers
  1. <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>"
  2.  
Here an example of edonkey link:

Expand|Select|Wrap|Line Numbers
  1. ed2k://|file|[Ubuntu.Linux.6.06].ubuntu-6.06-desktop-i386.iso|731744256|FB73A6DBAEFDFB7969FFD8CE8E4FC6E3|/
  2.  
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:

Expand|Select|Wrap|Line Numbers
  1. //######## INICIO HACK EDONKEY LINKS ########
  2.  
  3.  $text = ' ' . $text;
  4.  
  5.  $size = humanize_size(preg_replace("#(^|(?<=[^\w\"']))(ed2k://\|file\|([^\\/\|:<>\*\?\"]+?)\|(\d+?)\|([a-f0-9]{32})\|(.*?)/?)(?![\"'])(?=([,\.]*?[\s<\[])|[,\.]*?$)#i", "\\4", $text));
  6.  
  7.  $patterns[] = "#(^|(?<=[^\w\"']))(ed2k://\|file\|([^\\/\|:<>\*\?\"]+?)\|(\d+?)\|([a-f0-9]{32})\|(.*?)/?)(?![\"'])(?=([,\.]*?[\s<\[])|[,\.]*?$)#i";
  8.  
  9.  $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>";
  10.  
  11.  //######### FIM HACK EDONKEY LINKS ##########
  12.  

And here is the humanize_size function:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.     function humanize_size ($size, $rounder = 0, $min = '')
  4. {
  5.    $sizes   = array('Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
  6.    $rounders = array(0,   1,    2,    2,    2,    3,    3,    3,    3);
  7.    $ext = $sizes[0];
  8.    $rnd = $rounders[0];
  9.  
  10.    if ($min == 'KB' && $size < 1024)
  11.    {
  12.       $size = $size / 1024;
  13.       $ext = 'KB';
  14.       $rounder = 1;
  15.    }
  16.    else
  17.    {
  18.       for ($i=1, $cnt=count($sizes); ($i < $cnt && $size >= 1024); $i++)
  19.       {
  20.          $size = $size / 1024;
  21.          $ext  = $sizes[$i];
  22.          $rnd  = $rounders[$i];
  23.       }
  24.    }
  25.  
  26.    if (!$rounder)
  27.    {
  28.       $rounder = $rnd;
  29.    }
  30.  
  31.    return round($size, $rounder) . '&nbsp;' . $ext;
  32. }        
  33.  
  34. ?>
  35.  
Any help would be great.
Jun 23 '06 #1
Share this Question
Share on Google+
1 Reply


P: 2
UP! :) Help me please!
Jun 25 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.