Tags

,

PHP – preg_replace_callback

by ctrans on 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拍手

From Blog

2 Comments
  1. はじめまして。

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

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

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

  2. ctrans permalink

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

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS