Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Smarty SEO URL Friendly Modifier

 
Post new topic   Reply to topic    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
sangprabv
Smarty Rookie


Joined: 14 Feb 2005
Posts: 13
Location: Jakarta

PostPosted: Wed Aug 21, 2013 2:21 am    Post subject: Smarty SEO URL Friendly Modifier Reply with quote

The modifier:
Code:
<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage PluginsModifier
 */

/**
 * Smarty SEO URL Friendly modifier plugin
 *
 * Type:     modifier<br>
 * Name:     seo<br>
 * Purpose:  SEO URL Friendly
 *
 * @link http://smarty.php.net/manual/en/language.modifier.seo.php replace (Smarty online manual)
 * @author Willy Mularto <me at sangprabv dot web dot id>
 * @param string $string  input string
 * @param string $search  text to search for
 * @param string $delimiter replacement text
 * @param string $case uppercase/lowercase/ucfirst
 * @return string
 */
function smarty_modifier_seo($string, $search, $delimiter, $case)
{
   /*Replace any non latin chars*/
   $string = iconv('UTF-8', 'ASCII//TRANSLIT', $string);
    /*Remove any non alpha numeric*/
    $string = preg_replace("/[^a-zA-Z 0-9]+/", " ",$string);   
    /*Remove any double delimiter*/
    $string = preg_replace("/[[:blank:]]+/", "$delimiter",$string);
    /*Remove any delimiter at the beginning of string if any*/
    $string = ltrim($string, "$delimiter");
    /*Remove any delimiter at the end of string if any*/
    $string = rtrim($string, "$delimiter");
   
    switch($case) {
       case 'upper':
          return strtoupper($string);
       case 'lower':
          return strtolower($string);
       case 'first':
          return ucfirst($string);
       default:
          return($string);
    }
}

?>

Save as modifier.seo.php and store it in plugins folder
Call it on your template page, here is the example:
{-$title|seo:" ":"-":"first"-}
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Plugins All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP