Home arrow PHP arrow Page 5 - The preg_replace_callback() function in PHP

Using preg_replace_callback() in classes - PHP

The preg_replace_callback() function is an under-utilized and little documented PHP function that you will find useful and powerful in many situations, especially if you have been actively using preg_match() and preg_replace(). In this article, you will find a clear explanation of what this function does, together with carefully crafted examples that illustrate some of its uses.

  1. The preg_replace_callback() function in PHP
  2. preg_replace_callback() with conditional statements
  3. preg_replace_callback() with for-loops
  4. Using preg_replace_callback() to produce a running sequence
  5. Using preg_replace_callback() in classes
By: K.K.Sou
Rating: starstarstarstarstar / 3
September 09, 2008

print this article



Suppose you’re an object-oriented person that prefers using classes. Below is the corresponding code using the OOP (object-oriented programming) approach:


$str = "

list 1


<li>item A</li>

<li>item B</li>

<li>item C</li>


list 2


<li>item D</li>

<li>item E</li>

<li>item F</li>

<li>item G</li>


list 3


<li>item H</li>

<li>item I</li>

<li>item J</li>

<li>item K</li>

<li>item L</li>



$app = new ProcessList();

echo preg_replace_callback('%<ol>(.*?)</ol>%si', array(&$app, 'process_list'), $str);

class ProcessList {

function process_list($matches) {

global $sno;

$sno = 0;

return preg_replace_callback("/<li>(.*?)</li>/i", array(&$this, 'process_item'), trim($matches[1]));


function process_item($matches) {

global $sno;


return "$sno. $matches[1]";




Note the use of array(&$app, 'process_list') and array(&$this, 'process_item') in specifying the callback functions. The first parameter is the pointer to your object, and the second is the method name of the callback function in that object.

Final Words

We’ve come to the end of this article. In this tutorial, you have seen that the preg_replace_callback() function works exactly like the preg_replace() function. The only difference is that instead of specifying a replacement string, you specify a callback function. It is in the callback function that you can do many amazing things to produce your desired replacement string.

I hope the examples in this article have also get you excited about using the preg_replace_callback() function!

>>> More PHP Articles          >>> More By K.K.Sou

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: