CraigSherwood Posted November 13, 2007 Share Posted November 13, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/77210-solved-preg_replace_callback/ Share on other sites More sharing options...
effigy Posted November 13, 2007 Share Posted November 13, 2007 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}/ Quote Link to comment https://forums.phpfreaks.com/topic/77210-solved-preg_replace_callback/#findComment-390922 Share on other sites More sharing options...
CraigSherwood Posted November 13, 2007 Author Share Posted November 13, 2007 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);} ?> Quote Link to comment https://forums.phpfreaks.com/topic/77210-solved-preg_replace_callback/#findComment-391050 Share on other sites More sharing options...
effigy Posted November 14, 2007 Share Posted November 14, 2007 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> Quote Link to comment https://forums.phpfreaks.com/topic/77210-solved-preg_replace_callback/#findComment-391435 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.