PHP
  Home arrow PHP arrow Page 4 - The preg_replace_callback() function in PHP
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

The preg_replace_callback() function in PHP
By: K.K.Sou
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2008-09-09


    Table of Contents:
  • The preg_replace_callback() function in PHP
  • preg_replace_callback() with conditional statements
  • preg_replace_callback() with for-loops
  • Using preg_replace_callback() to produce a running sequence
  • Using preg_replace_callback() in classes

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    The preg_replace_callback() function in PHP - Using preg_replace_callback() to produce a running sequence
    ( Page 4 of 5 )

    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
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT