SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru DenverDave's Avatar
    Join Date
    Feb 2001
    Location
    Denver, Colorado
    Posts
    630
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Passing parameters to preg_replace_callback

    While perhaps somewhat esoteric, the preg_replace_callback function can be very useful to manipulate the characters in all matched strings with a function. The standard approach is to provide the pattern, function to be used (get_data) and the data to be analyzed.

    It would be vary useful for the function to have some other information, but I'm not sure how to pass additional parameters to get_data using the preg_replace_callback, so for now I've declared them global within get_data.

    Is there a way to pass additional parameters to the the preg_replace_callback function specified?

    Here is the php.net link:
    http://us3.php.net/manual/en/functio...e-callback.php

    Thanks.

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Globals are not good. Usually it's better to use some object's method as a callback.

    PHP Code:
    class T
    {
       function 
    replace($matches) {
          if(
    $this->param)...
       }
    }

    $t = new T;
    $t->param whatever;

    preg_replace_callback($pattern,
       array(&
    $t'replace'),
       
    $subject); 

  3. #3
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    not that i know of.

    another way to do it could be to make a call to another function to fetch some params inside of the callback func. you could have this other function serve as a data store, although im not sure this is any better than using globals. however, this would still work if your not working in the global scope, like for example, you are calling preg_replace_callback() inside of another function.

    PHP Code:
    function my_preg_callback($matches) {
        
    $params data_store();
        
    // use params
    }

    function 
    data_store($params null) {
        static 
    $data;
        if (
    $params) {
            
    $data $params;
        }
        return 
    $data;
    }

    // set some params
    data_store(array('foo' => 5'bar' => 1));
    preg_replace_callback(....., 'my_preg_callback');
    // change the data
    data_store(array('foo' => 8'bar' => 9));
    preg_replace_callback(....., 'my_preg_callback'); 

    you could do similar with objects.

    create_function() could also work too, but that would be very eval() like.

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh wow, i never knew you could use an object as the callback. thats definately the way go.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •