PHP
  Home arrow PHP arrow Page 5 - 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() in classes
    ( Page 5 of 5 )

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


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


    ";


    $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;

    ++$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
     

       

    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