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

PHP: preg_replace_callback

LEGEND ,
Nov 13, 2007 Nov 13, 2007

Copy link to clipboard

Copied

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




TOPICS
Server side applications

Views

216
Translate

Report

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 13, 2007 Nov 13, 2007

Copy link to clipboard

Copied

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


Votes

Translate

Report

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