PHP
  Home arrow PHP arrow Page 2 - Building a Site Engine with PHP, Part ...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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

Building a Site Engine with PHP, Part 4
By: James Murray
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


       · Suddenly the code is no longer indented. Is there a whitespace issue with PHP5 that...
       · I agree, this part 4 was really a disappointment.Hope the finale is a...
       · I must agree with the above commments.Since part 3 the quality of the articles has...
       · I hope things gets easier to understand when I've read the full article.As posted...
       · in article 3: where do I have to put the blocks and authentification plug-ins?in...
       · IN article 3:You have code for two different plugins, "auth" and "blocks".Now...
       · what about the replace() function? where do i have to put it? ... and that...
       · the load_template() function tells you exactly where your xml file...
       · I plan on rewriting this article due to the fact that it's a new writing style...
       · >AND yes, if you read all the articles with attention, you'd know what to do with...
       · First off, I don't consider myself an extremely knowledgeable user of PHP. That...
       · "What are the file names of the Templates and XML plug-ins?""... and XML class...
       · It's like trying to plug an episode of Jerry Springerinto Oprah show, they might...
       · Has anyone tried this yet? I know some are dissapointed, but I think that it's...
       · Ok, well Im treating this article as a guide to how to do it. I dont run PHP5 so I...
       · I agree that a download of the sample code would be FANTASTIC. I think that would...
       · Yeah thats pretty much it really, not the hardest thing I'll have done this month...
       · this IS a guide on how to do it, this isn't a "heres the source code for a finished...
       · I would like to thank you for writing these articles, the quality is going down a...
       · I'll say that- it got me thinking. Overall this has been a big eye-opener to me, as...
       · That is great that you the system up and running. Would it be possible for you to...
       · why would the sql function (inside sql.inc.php) not have any arrguments. I couldn't...
       · To get the sql function to work declare $config global in the function sql()...
       · This is my current core.inc.php, shows the changes made to support the sql inc...
       · So maybe you can add source to download ? with all files and mysql dump?
       · Thanks a bunch Simon. That seems to clear it all up. It’s nice to see some progress...
       · Well I made some progress on the parser today, it seems to do what the simpleXML...
       · simon ...wow... only 3 weeks... could you share with us what tutorials/books you've...
       · I'd really like to see a completed source package also.
       · as would I!
       · Well I dont have any PHP books, the online suport at php.net is very good as it is,...
       · a complete working package would be very helpful to understand how it works
       · The author made a comment saying he didn't want this to be a "heres a site download...
       · you read my mind :)
       · I'm studying this articles for a couple of hours and i have a question. You...
       · Any thoughts on the above comment?
       · I don't think that these articles should be always targeted toward beginners. If you...
       · Exactly, this is a guide, a tutorial. If you just want to download a working CMS,...
       · you can make them yourself in your core.inc.phpthis is my...
       · you can write small modules which creates an instance of the pluginLets look at...
       · Hi Simon,Ive been having some problems getting your PHP4 simple xml parser...
     

       

    PHP ARTICLES

    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT