Skip to main content
Inspiring
November 13, 2007
질문

PHP: preg_replace_callback

  • November 13, 2007
  • 1 답변
  • 238 조회
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




이 주제는 답변이 닫혔습니다.

1 답변

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
>
>
>
>