1

I have a function which slugifies the text, it works well except that I need to replace ":" with "/". Currently it replaces all non-letter or digits with "-". Here it is :

function slugify($text)
    {
        // replace non letter or digits by -
        $text = preg_replace('~[^\\pL\d]+~u', '-', $text);

        // trim
        $text = trim($text, '-');

        // transliterate
        if (function_exists('iconv'))
        {
            $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
        }

        // lowercase
        $text = strtolower($text);

        // remove unwanted characters
        $text = preg_replace('~[^-\w]+~', '', $text);

        if (empty($text))
        {
            return 'n-a';
        }

        return $text;
    }
themihai
  • 6,579
  • 11
  • 32
  • 53

1 Answers1

-1

I made just a couple modifications. I provided a search/replace set of arrays to let us replace most everything with -, but replace : with /:

$search = array( '~[^\\pL\d:]+~u', '~:~' );
$replace = array( '-', '/' );
$text = preg_replace( $search, $replace, $text);

And later on, this last preg_replace was replacing our / with an empty string. So I permited foward slashes in the character class.

$text = preg_replace('~[^-\w\/]+~', '', $text);

Which outputs the following:

// antiques/antiquities
echo slugify( "Antiques:Antiquities" );
Sampson
  • 251,934
  • 70
  • 517
  • 549
  • yeah I know that but what about the slugify function ? I mean that I could simple do a str_replace if I wanted just to replace ":" with "/" but I need to still slugify the string (remove special characters etc ) except that ":" should be replaced with "/" instead of "-". – themihai May 14 '12 at 03:33
  • @mihai You put this *inside* the slugify function, as part of the process. – Sampson May 14 '12 at 03:38
  • doesn't work. the ":" is still replaced by "-". I think it makes sense as we have $text = preg_replace('~[^\\pL\d]+~u', '-', $text); isn't is ? – themihai May 14 '12 at 03:47
  • @mihai What string are you passing into slugify? – Sampson May 14 '12 at 03:50
  • $text = 'Antiques:Antiquities'; echo slugify($text); I get antiques-antiquities – themihai May 14 '12 at 03:51
  • @mihai You had some competition between various `preg_replace` calls. – Sampson May 14 '12 at 04:40
  • @JonathanSampson why not just use `str_replace` for the colon replacement? – Kemal Fadillah May 14 '12 at 04:41
  • @KemalFadillah The OP can if they choose to. There is more than one way to remove a colon. – Sampson May 14 '12 at 04:41
  • @JonathanSampson yes, that's right . Actually that's the question... how to slugify the string with this special condition (replace ":" with "/" ). Your answer says to replace ":" with "/" which I find useless. I expected a complete regex . – themihai May 14 '12 at 05:42
  • @mihai You're asking how to slugify so that `:` becomes `/`, and when I tell you to swap the two in the process, you find that "useless"? Regular expressions match patterns; they don't magically replace various patterns with various substitution strings. Answers for this question may vary, but there's not a magical regular expression that will behave differently than every other regular expression in existence. – Sampson May 14 '12 at 05:46
  • @JonathanSampson I actually asked to "keep" the current regex except that ":" should be replaced with "/". In your original answer you provided just a method to replace ":" with "/" which broke the other regex rules . – themihai May 14 '12 at 05:50
  • @mihai Your other regular expressions were already in conflict. Your last regex wouldn't permit a `/`, which it why it needed to also be modified. – Sampson May 14 '12 at 05:53