Jump to content

[SOLVED] search through a string for various text


CraigSherwood

Recommended Posts

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);}
?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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);}  
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.