SEO Slug (PHP preg_replace equivalent)

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
ThomasFireheart
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 48
Joined: Sat May 24, 2008 2:27 pm
Location: Indiana, USA

SEO Slug (PHP preg_replace equivalent)

Post by ThomasFireheart » 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:

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
?>
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.

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: SEO Slug (PHP preg_replace equivalent)

Post by Thierry » Tue May 26, 2015 8:10 am

ThomasFireheart wrote:

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
?>
need some guidance on converting it to Livecode
Hi Thomas,

Here is a straight (and quick) translation but *NOT* tested.
Nice if you tell us if it works.

Code: Select all

function create_seo_slug T, theExtension
   local theReplace = "-"     // $replace = '-';
   put toLower( T) into T
   
   //replace / and . with white space
   put replaceText( T, "[/.]", space) into  T
   
   // $string = preg_replace("/[^a-z0-9_\s-]/", "", $string);  
   put replaceText( T, "[^a-z0-9_\s-]", empty) into  T
   
   //remove multiple dashes or whitespaces        [1]
   put replaceText( T, "[\s-]+", space) into  T
   
   //convert whitespaces and underscore to $replace
   put replaceText( T, "[\s_]", theReplace) into  T
   
   // limit the slug size
   put char 1 to 100 of T into T
   
   // if extension is specified, return string plus extension else return string.
   if theExtension is empty then return T
   return T & theExtension
end create_seo_slug
Greetings from France

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: SEO Slug (PHP preg_replace equivalent)

Post by Thierry » Tue May 26, 2015 4:25 pm

and if you like concise coding as I do,
here is a shorter one:

Code: Select all

function create_seo_slug_2 T, theExtension
   return char 1 to 100 of \
         replaceText( \
         replaceText( \
         replaceText( \
         replaceText( \
         toLower( T) , \
         "[/.]", space),     "[^\w\d_-\s]", empty) ,     "[\s-]+", space),     "[\s_]", "-") \   
         & theExtension
end create_seo_slug_2
Regards,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

ThomasFireheart
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 48
Joined: Sat May 24, 2008 2:27 pm
Location: Indiana, USA

Re: SEO Slug (PHP preg_replace equivalent)

Post by ThomasFireheart » Tue May 26, 2015 6:37 pm

That works wonderfully. I didn't know replaceText could handle regex. Thanks so much - Wish I could hand you one in person, but this will have to do :) http://beeroverip.org/london-porter/

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: SEO Slug (PHP preg_replace equivalent)

Post by Thierry » Tue May 26, 2015 7:57 pm

ThomasFireheart wrote:That works wonderfully. I didn't know replaceText could handle regex.
Great.
And regex in LiveCode works pretty well (PCRE library) :)
Thanks so much - Wish I could hand you one in person, but this will have to do :) http://beeroverip.org/london-porter/
Thanks for the beer, and for the second round, I'll offer you this one:
http://beeroverip.org/revolution/

Regards,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”