Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

PHP: run a function during preg_replace

LEGEND ,
Nov 09, 2007 Nov 09, 2007
This is an ongoing issue (sadly unsolved), if you're bored enough you can
see my other posts.
The code I have so far (thanks to one and all), successfully creates
hyperlinks that target the right files, have the right 'title', etc.
However, the getFileSize function refuses to work within the preg_replace.
Seems to me that I can't properly send the $1 variable to the getFileSize
function; the $strfilename always just shows up as $1, but really it should
be a file name, as it is within the hyperlink section. Most boring.
So, does anyone know how to trigger the getFileSize by sending $1 as a
string rather than just as a variable name '$1'?
When called separately the function getFileSize works, so the issue really
is the getFileSize('$1') section.

<?php function changeMe($str){
$pattern = "/\{([^:]*):([^\}]*)\}/";
preg_match($pattern,$str,$matches);
$file = $matches[2];
$pattern = "/\{link:([^\}]*)\}/";
return preg_replace($pattern,'<a href="docs/$1" title="click to open $1"
target="_blank">'.getFileSize('$1').'</a>',$str);}
?>
<?
function getFileSize($strfilename){
echo $strfilename.'<br />'; //returns $1 but should really return the file
name
$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);}
?>


TOPICS
Server side applications
309
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 10, 2007 Nov 10, 2007
Did you see Michael Fesser's reply in an earlier thread about
preg_replace_callback()?
it seems like that's what you need.

"Craig" <csintheuk@hotmail.com> wrote in message
news:fh395r$p3l$1@forums.macromedia.com...
> This is an ongoing issue (sadly unsolved), if you're bored enough you can
> see my other posts.
> The code I have so far (thanks to one and all), successfully creates
> hyperlinks that target the right files, have the right 'title', etc.
> However, the getFileSize function refuses to work within the preg_replace.
> Seems to me that I can't properly send the $1 variable to the getFileSize
> function; the $strfilename always just shows up as $1, but really it
> should be a file name, as it is within the hyperlink section. Most boring.
> So, does anyone know how to trigger the getFileSize by sending $1 as a
> string rather than just as a variable name '$1'?
> When called separately the function getFileSize works, so the issue really
> is the getFileSize('$1') section.
>
> <?php function changeMe($str){
> $pattern = "/\{([^:]*):([^\}]*)\}/";
> preg_match($pattern,$str,$matches);
> $file = $matches[2];
> $pattern = "/\{link:([^\}]*)\}/";
> return preg_replace($pattern,'<a href="docs/$1" title="click to open $1"
> target="_blank">'.getFileSize('$1').'</a>',$str);}
> ?>
> <?
> function getFileSize($strfilename){
> echo $strfilename.'<br />'; //returns $1 but should really return the file
> name
> $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);}
> ?>
>
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 10, 2007 Nov 10, 2007
Thanks Joris,
the preg_replace_callabck() has me thoroughly confused, but I guess a few
more sleepless nights and I'll either have cracked it or moved to
Kyrgyzstan.
Craig

"Joris van Lier" <whizzrd@hotmail.com> wrote in message
news:fh3rgb$gob$1@forums.macromedia.com...
> Did you see Michael Fesser's reply in an earlier thread about
> preg_replace_callback()?
> it seems like that's what you need.
>
> "Craig" <csintheuk@hotmail.com> wrote in message
> news:fh395r$p3l$1@forums.macromedia.com...
>> This is an ongoing issue (sadly unsolved), if you're bored enough you can
>> see my other posts.
>> The code I have so far (thanks to one and all), successfully creates
>> hyperlinks that target the right files, have the right 'title', etc.
>> However, the getFileSize function refuses to work within the
>> preg_replace.
>> Seems to me that I can't properly send the $1 variable to the getFileSize
>> function; the $strfilename always just shows up as $1, but really it
>> should be a file name, as it is within the hyperlink section. Most
>> boring.
>> So, does anyone know how to trigger the getFileSize by sending $1 as a
>> string rather than just as a variable name '$1'?
>> When called separately the function getFileSize works, so the issue
>> really is the getFileSize('$1') section.
>>
>> <?php function changeMe($str){
>> $pattern = "/\{([^:]*):([^\}]*)\}/";
>> preg_match($pattern,$str,$matches);
>> $file = $matches[2];
>> $pattern = "/\{link:([^\}]*)\}/";
>> return preg_replace($pattern,'<a href="docs/$1" title="click to open $1"
>> target="_blank">'.getFileSize('$1').'</a>',$str);}
>> ?>
>> <?
>> function getFileSize($strfilename){
>> echo $strfilename.'<br />'; //returns $1 but should really return the
>> file name
>> $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);}
>> ?>
>>


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 10, 2007 Nov 10, 2007


"Craig" <csintheuk@hotmail.com> wrote in message
news:fh4l22$jji$1@forums.macromedia.com...
> Thanks Joris,
> the preg_replace_callabck() has me thoroughly confused, but I guess a few
> more sleepless nights and I'll either have cracked it or moved to
> Kyrgyzstan.
> Craig

Yeah I know, living in Confuzistan isn't what it used to be ehh :)

What is confusing you?

The general idea is that every time a match is found, a callback to a custom
function with the value of the match is performed.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 10, 2007 Nov 10, 2007
LATEST
Joris, David & Michael,
finally - its a thing of beauty - and fully functioning.
Many thanks for all your help.
Below is what works.

Craig


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


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines