Skip to main content
Inspiring
November 13, 2007
Question

PHP: preg_replace_callback

  • November 13, 2007
  • 1 reply
  • 238 views
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




This topic has been closed for replies.

1 reply

Inspiring
November 13, 2007
In case anyone's interested, the answer seems to be:-

<?php function changeMe($str){
$str = preg_replace_callback('/\{@([^\}]*)\@}/','getFileSize',$str);
$str = preg_replace_callback('/\{:([^\}]*)\:}/','getName',$str);
return $str;
}
?>

No doubt there's a more elegant solution, but for now ... at least it works.

"Craig" <csintheuk@hotmail.com> wrote in message
news:fhcuq5$km8$1@forums.macromedia.com...
> 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
>
>
>
>