PHP
  Home arrow PHP arrow Page 2 - Building a Site Engine with PHP, Part 4
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? 
Google.com  
PHP

Building a Site Engine with PHP, Part 4
By: James Murray
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 24
    2004-07-12


    Table of Contents:
  • Building a Site Engine with PHP, Part 4
  • You’ve been Tagged
  • Give it a Little Class:
  • The Final Words

  • 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


    Building a Site Engine with PHP, Part 4 - You’ve been Tagged
    ( Page 2 of 4 )

    The tagging system, as I like to call it, is one of the biggest parts of the entire engine, not in size, rather, in functionality. Through out the entire engine we have things being sent to the replace() function, which is the function that replaces all the tags with the correct data, before it’s sent to the browser. So with no further interruptions, let's look at the replace function.

    First we define the function and all the parameters it will accept. $load is the array of tags to replace. The array should be set up like this array(‘tagname’=>”data to place where the tag is”); $string is the variable that holds the XML string that we’ll later get from our template using simpleXML; and $mask is an optional variable that you can use in case you want to use a different tag style, such as <*tag>.

    function replace($load,$string,$mask='<!-tag->'){

    Then we make sure that $load is an array so we don’t get any nasty unexpected errors. If it’s not an array, the function will just return the XML string without processing it:

    if(!is_array($load)){
    return $string;
    }else{

    If $load is an array, first make sure that there isn’t a tag in the $load named “-tag-“. Then explode the $mask tag by “-tag-“, so that we have an array that has the opening and closing brackets for each tag, “<!” and “>”. Now you might say “why all this stuff about the tags being explode to simply get the ‘<!’ and ’>’.” The reason behind that is like I said before, so you can change the tag style to something else like <*-tag-> if you want to or need too.

    if(!strpos(@$load,'-tag-')) $mask='<!-tag->';
    $masks=explode('-tag-',$mask);

    Just so you know the part of the previous lines of code “!strpos(@$load,'-tag-')” will produce a warning through PHP because we’re reading an array as if it were a string, however it still works as expected.

    Now we’re get to use my favorite PHP functions, array_keys(). array_keys() returns the keys of the input array as the values of the output array. We do this because we need to get the tag name so that we can then build our tag in the function to match it to the tag in the $string to replace it with our data.

    Anyway, now we’ll use array_keys() to get all the names of the tags from the $load array we load that into $tags . Then we run the $tags array through a loop to get the value of each of the array keys. Now we build our new list of the actual tags to replace (this is a little easier than searching the XML document to find all the replaceable tags).

    $tags=array_keys($load);
    foreach($tags as $tag){
    $newtags[]=$masks[0].$tag.$masks[1];
    }

    Now we use another built in function that’s just like array_keys() but instead of operating on the keys it operates on the values. It’s surprisingly named array_values(). Then we put it all together and run it through srt_replace() which will loop through the arrays that we pass to it. Str_replace() will return a string with all occurrences of $newtags in $string replaced with values from $load respectively.

    $loads=array_values($load);
    return str_replace($newtags,$loads,$string);
    }
    }

    That’s about it for the replace() function. Now let's move on to how we plan on getting the XML elements from our template to the replace() function.



     
     
    >>> More PHP Articles          >>> More By James Murray
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek