Everybody wants their page is crawled and indexed by popular search engine, one tips is to make your page looks like http://yourdomain.tld/page/read/this-is-my-article rather than http://yourdomain.tld/detail.php?id=12345 and people usually call it SEO URL Friendly

I would like to share my Smarty SEO URL Friendly Custom Modifier. This modifier is developed under Smarty 3.1.1.4, and should be compatible to all 3.x version.

Copy paste this modifier:
<?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) (I wish)
* @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 it as modifier.seo.php and store in your Smarty’s plugin directory.

How to call the modifier? You just need to call it {$title|seo:” “:”-“:”first”} on your template page. Let me explain it part by part,

  • { and } is of course your Smarty tag delimiter
  • $title is your variable (change it to yours)
  • | is delimiter when you want call a modifier
  • seo is our modifier name
  • : is the modifier’s property delimiter
  • ” ” is the space
  • “-” is the delimiter to replace the space
  • “first” is if you want to capitalize every first letter (you can use upper if you want to uppercase all, or lower if you want to lowercase all)

Easy right?

Bagikan halaman ini
Kategori: PHP

0 komentar pada “Smarty SEO URL Friendly Custom Modifier”

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

Seo wordpress plugin by www.seowizard.org.