SEO Slug (PHP preg_replace equivalent)
Posted: Tue May 26, 2015 2:01 am
I need to create a SEO slug from a bit of text - Currently, I use the following PHP code but need some guidance on converting it to Livecode:
Does anyone have any guidance on how to easily make a livecode equivalent of the PHP preg_replace ? All I need to do duplicate the above code so that it runs in a livecode app and generates the same/correct seo slug url's.
Code: Select all
<?php
function create_seo_slug($string, $ext='.html'){
$replace = '-';
$string = strtolower($string);
//replace / and . with white space
$string = preg_replace("/[\/\.]/", " ", $string);
$string = preg_replace("/[^a-z0-9_\s-]/", "", $string);
//remove multiple dashes or whitespaces
$string = preg_replace("/[\s-]+/", " ", $string);
//convert whitespaces and underscore to $replace
$string = preg_replace("/[\s_]/", $replace, $string);
//limit the slug size
$string = substr($string, 0, 100);
//slug is generated
// if extension is specified, return string plus extension else return string.
return ($ext) ? $string.$ext : $string;
}
//INPUT
$string = "what is your name ?";
$slug = create_seo_slug($string);
//OUTPUT::
//what-is-your-name.html
?>