Jump to content

[SOLVED] preg_replace_callback


CraigSherwood

Recommended Posts

Does anyone know to how write this properly, or indeed if it can be done?

 

In plain English ......

if $pattern = "/\{@([^\}]*)\@}/";
return preg_replace_callback($pattern,'getFileSize',$str);
else
if $pattern = "/\{[^\}]*)\:}/";
return preg_replace_callback($pattern,'getName',$str);

 

Trying to search for two possible string combinations within a larger string

and run different callbacks depending on the result, within the same larger

string.

I suspect I may have to break out of the callback 'getFileSize' and branch

to 'getName' if the $pattern is wrong, but damned if I know how.

 

 

Craig

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/77210-solved-preg_replace_callback/
Share on other sites

Just run each function. The callbacks will only be called if the pattern matches.

 

preg_replace_callback('/\{@([^\}]*)\@}/', 'getFileSize', $str);
preg_replace_callback('/\{[^\}]*)\:}/', 'getName', $str);

 

You can consolidate these by capturing the "@" or the ":", then checking this within the callback to run different processing.

 

/\{([@:])([^\}]*)\1}/

Many thanks for the pointer. Though the code below is obviously clumsy, clunky and no doubt repetitive, it does at least work. Thank you.

I know what you mean about capture the @ and the :, but I have no idea how to do it...

again, many thanks.

Craig

 

<?php function changeMe($str){
$str = preg_replace_callback('/\{@([^\}]*)\@}/','getFileSize',$str);
$str = preg_replace_callback('/\{[^\}]*)\:}/','getName',$str);
return $str;
}
?>
<? function getName($strfilename){
$pathname='docs/';
$fullname=$pathname.$strfilename[1];
    if (file_exists($fullname)){ 
        $strsize = human(filesize($fullname));
	$name = $strfilename[1];
	$newlink = '<a href="'.$fullname.'" title="click to open '.$name.'" target="_blank">here ('.$strsize.')</a>';
	return $newlink;
    } else { 
        return("Sorry, the file ".$strfilename[1]." does not exist."); 
    } 
   return;  
}
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("Sorry, the file ".$strfilename[1]." does not exist."); 
    } 
   return;  
} 
function human($size){ 
   for($si = 0; $size >= 1024; $size /= 1024, $si++); 
   return round($size, 1).substr(' kMG', $si, 1);}  
?>

I know what you mean about capture the @ and the :, but I have no idea how to do it...

 

<pre>
<?php
$str = '{@atsign@}{:colon:}';
preg_replace_callback('/\{([@:])([^\}]*)\1}/', 'callback', $str);
function callback ($matches) {
	switch ($matches[1]) {
		case '@':
			echo "@ with $matches[2]<br>";
		break;
		case ':':
			echo ": with $matches[2]<br>";
		break;
	}
}
?>
</pre>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.