View Single Post
  #8  
Old 10-07-2002, 07:58 AM
  minorgod's Avatar 
minorgod minorgod is offline
 

X-Adept
  
Join Date: Sep 2002
Location: Arivaca, AZ
Posts: 402
 

Default Eureka!

Okay, below is a new Smarty Output Filter I wrote to rewrite all the links on a page to be search-engine friendly. HOWEVER, you cannot simply drop this in your Smarty Plug-ins folder and be done. If you rewrite all your page links using this plug-in, then you'll still need to alter your application logic to handle GET varables differently, so you can parse them from the PATH_INFO rather than from the _GET or HTTP_GET_VARS array. Here is the code to make your own Smarty Plug-in....

Code:
<?php /* * Smarty plugin * ------------------------------------------------------------- * File: outputfilter.make_searchable.php * Type: outputfilter * Name: make_searchable * Version: 1.0 * Date: October 7, 2002 * Purpose: To rewrite all the links on your page to a search engine friendly format. If you use this filter, you will also need to change the logic of your php source to handle GET variables differently. This script attempts to help you do this by filling in any unset GET variables with the value "undefined" so you have an easier time matching up the variables with their values. * Install: Drop into the plugin directory, then in your php pages call * $smarty->load_filter('output','make_searchable'); * just before you call your display such as $smarty->display("customer/home.tpl"); * Author: Brett R. Brewer < brett@brettbrewer.com, brett@realestate-school.com > * ------------------------------------------------------------- */ function smarty_outputfilter_make_searchable($source, &$smarty) { //now lets build an array of all the link nodes in the source // Pull out the links and replace them with markers for later use preg_match_all("!<a[^>]+>.*?</a>!is", $source, $match); $_anchor_tags = $match[0]; $source = preg_replace("!<a[^>]+>.*?</a>!is",'@@@SMARTY:TRIM:HREF@@@', $source); //now strip out the actual href values //fix any unfilled query values by inserting the value as "undefined" foreach($_anchor_tags as $curr_anchor){ //replace any hanging query arguement at the end of the string //which we can tell if it ends with =" or theres an =# or =& $curr_anchor=preg_replace("/(=\x22>)|(=\x27>)/","=undefined\">",$curr_anchor); //replaces dangling = $curr_anchor=preg_replace("/=#/","=undefined#",$curr_anchor); //replaces =# situations $curr_anchor=preg_replace("/=&/","=undefined&",$curr_anchor); //replaces =& situations $fixed_anchors[]=$curr_anchor; } //now strip out the actual href values including quotes, replacing any url parameter //characters with slashes foreach($fixed_anchors as $fixedanchor) { preg_match("!(\"|')([^\"']+)(\"|')!is", $fixedanchor, $url_value); $url_values[]=preg_replace("/[&=#?]+/","/",$url_value[0]); } //now replace the old http value with the new slashed version foreach($fixed_anchors as $key=>$value){ $fixed_anchors[$key]=preg_replace("/(\"|')([^\"']+)(\"|')/","$url_values[$key]",$value,1); } //now replace the original anchor tags with the new ones... foreach($_anchor_tags as $key=>$value){ $_anchor_tags[$key]=$fixed_anchors[$key]; } // replace all the markers in the source code with the modified links foreach($_anchor_tags as $curr_anchor) { $source = preg_replace("!@@@SMARTY:TRIM:HREF@@@!",$curr_anchor,$source,1); } //now return the modified source code return $source; } ?>

Please note, this code is not optimized. A couple of steps here could easily be reduced into a single step, but I needed the extra steps for easier debugging. If you modify this or discover bugs, please post them here. If you need to see the code that is being generated without rendering it in your browser, simply change the last line of this output filter to
Code:
return htmlspecialchars($source);
and it will fill your browser with source code rather than rendered code. Very helpful for debugging.

Below is a sample PHP script that you might use to grab the variable names and values from the URL on pages where you use this output filter, or target pages you might navigate to from a page using this output filter:

Code:
<?php /* A simple script to translate path-ized URL parameters back into variables. */ //check for existence of $PATH_INFO if(isset($PATH_INFO)) { //create an array of the variable names and their values $vardata = explode('/', $PATH_INFO); //count the number of parameters $num_param = count($vardata); //if the number of parameters is even add an extra empty element //to simplify array traversal if($num_param % 2 == 0) { $vardata[] = ''; $num_param++; } //now loop through the array and create a variable //with the correct name for each value and set the value for($i = 1; $i < $num_param; $i += 2) { $$vardata[$i] = $vardata[$i+1]; } } ?>

Love me.
__________________
www.brettbrewer.com
Getting back into x-cart dev after a long hiatus. Modded lots of x-carts from version 3.1.x to 4.1.x. Developer of ImageScaler mod, Pre-login per user coupon mod, Wordpress feed mod, DigitalSubscriptions mod, Phonetic bulk download keys addon for DownloadExpander mod, Serial Number Generator for ESD products, Custom CMS/LMS integrations, external products mod, and more.
Reply With Quote