note 107112 modified in function.preg-replace by danbrown
| From: | danbrown@php.net | Date: | Mon, 09 Jan 2012 18:28:48 +0000 |
| Subject: | note 107112 modified in function.preg-replace by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-185482@lists.php.net to get a copy of this message | ||
To covert a string to SEO friendly, do this:
<?php
$realname = "This is the string to be made SEO friendly!"
$seoname = preg_replace('/\%/',' percentage',$realname);
$seoname = preg_replace('/\@/',' at ',$seoname);
$seoname = preg_replace('/\&/',' and ',$seoname);
$seoname = preg_replace('/\s[\s]+/','-',$seoname); // Strip off multiple spaces
$seoname = preg_replace('/[\s\W]+/','-',$seoname); // Strip off spaces and
non-alpha-numeric
$seoname = preg_replace('/^[\-]+/','',$seoname); // Strip off the starting
hyphens
$seoname = preg_replace('/[\-]+$/','',$seoname); // // Strip off the ending
hyphens
$seoname = strtolower($seoname);
echo $seoname;
?>
This will print: this-is-the-string-to-be-made-seo-friendly
--was--
To covert a string to SEO friendly, do this:
$realname = "This is the string to be made SEO friendly!"
$seoname = preg_replace('/\%/',' percentage',$realname);
$seoname = preg_replace('/\@/',' at ',$seoname);
$seoname = preg_replace('/\&/',' and ',$seoname);
$seoname = preg_replace('/\s[\s]+/','-',$seoname); // Strip off multiple spaces
$seoname = preg_replace('/[\s\W]+/','-',$seoname); // Strip off spaces and
non-alpha-numeric
$seoname = preg_replace('/^[\-]+/','',$seoname); // Strip off the starting
hyphens
$seoname = preg_replace('/[\-]+$/','',$seoname); // // Strip off the ending
hyphens
$seoname = strtolower($seoname);
echo $seoname;
This will print: this-is-the-string-to-be-made-seo-friendly
http://php.net/manual/en/function.preg-replace.php