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

Using preg_replace_callback() to produce a running sequence - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

We will now take a look at an even more interesting example doing a preg_replace using a regular expression with a running sequence.

Suppose you have an ordered list in HTML format:


list 1

<ol>

<li>item A</li>

<li>item B</li>

<li>item C</li>

</ol>


You would like to convert this into a plain text format in running sequence:


list 1

1. item A

2. item B

3. item C


Below is the code using preg_replace_callback():


<?php

$str = "list 1

<ol>

<li>item A</li>

<li>item B</li>

<li>item C</li>

</ol>

";


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


function process_list($matches) {

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

}


function process_item($matches) {

static $sno=0;

++$sno;

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

}


?>


Note the use of two preg_replace_callback() functions. The first one is used to grab all the items within the tags <ol> and </ol>. The second one is used to process each individual item, in which we append a running serial number in front of each item.

In case you have multiple lists such as:


list 1

<ol>

<li>item A</li>

<li>item B</li>

<li>item C</li>

</ol>


list 2

<ol>

<li>item D</li>

<li>item E</li>

<li>item F</li>

<li>item G</li>

</ol>


list 3

<ol>

<li>item H</li>

<li>item I</li>

<li>item J</li>

<li>item K</li>

<li>item L</li>

</ol>


you have to change the code slightly to accommodate this:


<?php

$str = "

list 1

<ol>

<li>item A</li>

<li>item B</li>

<li>item C</li>

</ol>


list 2

<ol>

<li>item D</li>

<li>item E</li>

<li>item F</li>

<li>item G</li>

</ol>


list 3

<ol>

<li>item H</li>

<li>item I</li>

<li>item J</li>

<li>item K</li>

<li>item L</li>

</ol>


";


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


function process_list($matches) {

global $sno;

$sno = 0;

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

}


function process_item($matches) {

global $sno;

++$sno;

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

}


?>


Note the two key changes here. First, we have used the non-greedy quantifier *? so that each list remains as one unit. Second, we need to think of some way to make the serial number restart from one for each new list. Here, for simplicity, I just use a global variable.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- 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: