CraigSherwood Posted November 8, 2007 Share Posted November 8, 2007 My users enter text into a database field in the form of: this file {link:afile.pdf} is big and this file {link:another.doc} is small. I have no idea how many times they may enter {link:xxx} into a given field. The code we worked out (see other post below) fabulously translates the text into a hyperlink. The problem is that no matter how often the {link:xxx} is in a given field, I only ever get the first file returned; i.e: This file afile.pdf is big and this file afile.pdf is small. But what I should get is, obviously: This file afile.pdf is big and this file another.doc is small. I need to use some kind of looping function, or a foreach type function. Does anyone have any clues? The code is currently: <? function getFileSize($strfilename) { $pathname='docs/'; $fullname=$pathname.$strfilename; if (file_exists($fullname)){ $strsize = human(filesize($fullname)); return($strfilename.' : '.$strsize); } else { return("no file found"); } return; } function human($size) { for($si = 0; $size >= 1024; $size /= 1024, $si++); return round($size, 1).substr(' kMG', $si, 1); } ?> <?php function changeMe($str){ $pattern = "/\{([^:]*)[^\}]*)\}/"; preg_match($pattern,$str,$matches); $file_ext = $matches[2]; $pattern = "/\{link:([^\}]*)\}/"; return preg_replace($pattern,'<a href="docs/\1" title="click to open '.$file_ext.'" target="_blank">'.getFileSize($file_ext).'</a>',$str);} ?> Quote Link to comment Share on other sites More sharing options...
CraigSherwood Posted November 9, 2007 Author Share Posted November 9, 2007 Most bizarre. Noticed that the right files are being linked to, but the wrong text is being displayed. Which means if I could figure out how to extract the \1 part in the hyperlink of this code <?php function changeMe($str){ $pattern = "/\{([^:]*)[^\}]*)\}/"; preg_match($pattern,$str,$matches); $file_ext = $matches[2]; $pattern = "/\{link:([^\}]*)\}/"; return preg_replace($pattern,'<a href="docs/\1" title="click to open '.$file_ext.'" target="_blank">'.getFileSize($file_ext).'</a>',$str);} ?> all would be well. Obviously the $file_ext = $matches[2] section needs to be some kind of an array as opposed to being set in stone. And yet, the \1 is already being successfully looped. However, I've been trying for hours and nothign seems to work. Any clues? Quote Link to comment Share on other sites More sharing options...
CraigSherwood Posted November 10, 2007 Author Share Posted November 10, 2007 Finally, its solved courtesy (in part) by users of this site and the Macromedia site. Users enter into a MySQL field text, which could contain multiple versions of {link:xxx}. For example: Here's a file {link:file.pdf} and here's another {link:hello.doc} The following code will look at the string and return it as formatted hyperlinks with the size of each document as well. <?php function changeMe($str){ $pattern = "/\{link:([^\}]*)\}/"; return preg_replace_callback($pattern,'getFileSize',$str);} ?> <? function getFileSize($strfilename){ $pathname='docs/'; $fullname=$pathname.$strfilename[1]; if (file_exists($fullname)){ $strsize = human(filesize($fullname)); $name = $strfilename[1]; $namesize = $strfilename[1].' : '.$strsize; $newlink = '<a href="'.$fullname.'" title="click to open '.$name.'" target="_blank">'.$namesize.'</a>'; return $newlink; } else { return("no file found"); } return; } function human($size){ for($si = 0; $size >= 1024; $size /= 1024, $si++); return round($size, 1).substr(' kMG', $si, 1);} ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.