programming.torensma.net: Code Snippets

Using preg_replace_callback on class methods

To prevent an error likeĀ preg_replace_callback() [function.preg-replace-callback]: Requires argument 2, ‘function_name’, to be a valid callback, use the following snippet:

$text = preg_replace_callback(
    '((http(s?)\://){1}\S+)',

    array(get_class($this), 'function_name'),
    $text
);

The trick is to call the method like so:

array(get_class($this), '__shortUrl')

You can follow any responses to this entry through the RSS 2.0 feed.