PHP – preg_replace_callback

By ctrans | 2010-07-16

preg_replace_callback() でクラス内の関数をコールバック関数とする場合

class foo {
  function mogemoge($matches){
    ・・・
  }
  function hogehoge(){
    ・・・
    preg_replace_callback( $pattern, array($this,"mogemoge"), $str ) ;
  }
}

array($this,”mogemoge”)のように配列で渡す。はまったのでメモ。

web拍手

2 Comments

sutara_lumpur on 2010-07-28 at 2:26 PM.

はじめまして。

試してみましたが、『array()』を囲むシングルクオートは
必要ないのではないでしょうか?
CakePHP上で試したのですが、

preg_replace_callback( $pattern, array($this,”mogemoge”), $str );

こう記述しなければエラーになってしまいました。

ctrans on 2010-07-28 at 2:35 PM.

ご指摘ありがとうございます。早速修正いたします!

Leave Your Comment

Your email will not be published or shared.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">