Jump to content

[SOLVED] display links from database, constructs and expressions


CraigSherwood

Recommended Posts

My users need to enter links into a MySQL text field (along with their text) to a file on the server.

For example;

the file <a href="somefile.pdf"> somefilea is really big.

However, I also want to calculate the file size for them.

I've used some of your code to do this, <?php getFileSize('somefile.pdf') ?>, so that the user would enter their text as;

the file size <?php getFileSize('cover.pdf') ?> is really big.

However, I've been told that putting PHP code in the database field is both problematic and requires the use of eval().

But, I've also been told that I can use something like:

the file {link:somefile.pdf} is really big.

Then, when the text appears on the page a script looks through the text and constructs an html link from the text {link:somefile.pdf}.

But ... how? What would that script look like, any clues?

 

 

Link to comment
Share on other sites

Finally, looks like I'm getting somewhere here, thank you.

However, the file name will be unknown to the script, as will the text either side of it, thus I presume that part of your code would actually be

<?php $str = '{link:somefile.pdf}';

That still, of course, leaves the question of the unknown file name. In other words the script needs to divide up the $str= section into two separate pieces, and then create an html link combined the getfilesize for url part.

 

Any clues?

 

 

 

Link to comment
Share on other sites

Assuming the following format:

 

$str = 'the file {link:somefile.pdf} is really big';

 

The following will break the $str variable into the following pieces; type of html (IE: link), filename (with and without extension for displaying link text)

$pattern = "/\{([^:]*)[^\}]*)\}/";
preg_match($pattern,$str,$matches);
$html_output = $matches[1];//first parentheses
$file_ext = $matches[2];//second parentheses
list($filename,$extension) = explode(".",$file_ext);
$filename = ucfirst($filename);//capitalize the first letter of the filename
$extension = strtoupper($extension);//capitalize the whole extension, so it looks like PDF or TXT

 

The following code will not give you the results, but rather, replace the matching pattern with a link (under the assumption that $html_output is 'link') as it is in the following code:

$pattern = "/\{link:([^\}]*)\}/";
//$replace = '<a href="\1">\1 - '.getFileSize("\1").'</a>';
//shown $replace so you can easily look at it and see how it's formatted, \1 would be filename.pdf
preg_replace($pattern,'<a href="\1">\1 - '.getFileSize("\1").'</a>',$str);

 

It will create a link similar to:

 

the file <a href='filename.pdf'>filename.pdf - 1024KB</a> is really big

Link to comment
Share on other sites

Well, it kind of works and kind of doesn't. Which is entirely my fault and I humbly apologise for my ignorance. But ....

 

1. When a user enters the text {link:afile.pdf} into the database, they may equally be adding {link:whyme.doc}; I have no way of knowing, thus I can't specify an actual file name for the variable $str.

 

2. Where do I put your script, I have tried it in a linked file, i.e I have this <?php include("phpfunctions.php"); ?> in the top of my files. And it certainly works in the sense that I get a message stating that there is "no such file" - that information comes from my script getFileSize. And yet, the file does exist because if I have this in the page <?php getFileSize('cover.pdf'); ?>, it actually displays "cover.pdf : 22M".

 

3. Even though the script partially works I must have it in the wrong place because nothing is bieng replaced anywhere, so, how do I call/trigger the script to run? The nearest I can get is by adding echo to  preg_replace, but that merely puts it in the first line and replaces nothing.

 

Sorry, to be such a pain, but I'm utterly confused. ???

 

Craig

 

Link to comment
Share on other sites

Well, when you read the string, place this php before so it modifies the output.

 

Try echoing out some of the variables in the script to make sure it's collecting it all correctly

 

$str = "put something here for me!";

$pattern = "/\{([^:]*)[^\}]*)\}/";
preg_match($pattern,$str,$matches);
$html_output = $matches[1];//first parentheses
$file_ext = $matches[2];//second parentheses
list($filename,$extension) = explode(".",$file_ext);
$filename = ucfirst($filename);//capitalize the first letter of the filename
$extension = strtoupper($extension);//capitalize the whole extension, so it looks like PDF or TXT

$echoing = "Filename w/ extension: ".$file_ext."<br>";
$echoing .= "Method of html, (link, etc..): ".$html_output."<br>";
$echoing .= "Filename w/o extension: ".$filename."<br>";
$echoing .= "Type of file: ".$extension;

$pattern = "/\{link:([^\}]*)\}/";
//$replace = '<a href="\1">\1 - '.getFileSize("\1").'</a>';
//shown $replace so you can easily look at it and see how it's formatted, \1 would be filename.pdf
$strs = preg_replace($pattern,'<a href="\1">\1 - '.getFileSize("\1").'</a>',$str);

echo $echoing."<hr>";
echo "Old String: ".$str."<hr>";
echo "New String: ".$strs;

 

Basically, at the top of this code, just get a string that would be what it needs to read... Run that script, see what echo's out, and if it's finding the correct stuff, replacing, etc... This will completely separate the code out and see where it goes wrong, if it does.

Link to comment
Share on other sites

Many thanks. Slowly but surely making progress. I have taken sections of your code and put it into my included phpfunctions.php file; like so

<? 
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
$str = "{link:cover.pdf}";
$pattern = "/\{([^:]*)[^\}]*)\}/";
preg_match($pattern,$str,$matches);
$file_ext = $matches[2];
$pattern = "/\{link:([^\}]*)\}/";
preg_replace($pattern,'<a href="docs/\1">'.getFileSize($file_ext).'</a>',$str);
?>

That certainly works but only if

a. You happen to know the $str file name, i.e NOT if all you know is to look for {link:, ending with }

b. You call the script from somewhere, like this:

<?php echo nl2br(changeMe($row_rsGen['txt6'])); ?>

 

and you alter the script to a function, like this:

<?php function changeMe(){
$str = "{link:cover.pdf}";
$pattern = "/\{([^:]*)[^\}]*)\}/";
preg_match($pattern,$str,$matches);
$file_ext = $matches[2];
$pattern = "/\{link:([^\}]*)\}/";
return preg_replace($pattern,'<a href="docs/\1">'.getFileSize($file_ext).'</a>',$str);}
?>

But then all the other text in the table-field gets wiped out. The field has in it: This file {link:cover.pdf} is really big. But all I get in the page is: cover.pdf : 22M (admittedly as a hyperlink as I wanted).

 

So ...

how to get unknown files to be found?

how to get the scrpt to run without deleting all text NOT in the $str?

 

Link to comment
Share on other sites

GOT IT. Finally and many thanks.

I have this in my included file:

	<? 
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">'.getFileSize($file_ext).'</a>',$str);}
?>

and I have this on the page itself

<?php echo nl2br(changeMe($row_rsGen['txt6'])); ?>

Seems that I can now find any file, as long as it is in the right folder of course.

Many, many thanks.

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.