12

I'm writing my own component for Joomla 1.5. I'm trying to figure out how to generate an "alias" (friendly URL slug) for the content I add. In other words, if the title is "The article title", Joomla would use the-article-title by default (you can edit it if you like).

Is there a built-in Joomla function that will do this for me?

DisgruntledGoat
  • 70,219
  • 68
  • 205
  • 290

4 Answers4

34

Line 123 of libraries/joomla/database/table/content.php implements JFilterOutput::stringURLSafe(). Pass in the string you want to make "alias friendly" and it will return what you need.

jlleblanc
  • 3,510
  • 25
  • 26
4

If you are trying to generate an alias for your created component it is very simple. Suppose you have click on save or apply button in your created component or suppose you want to make alias through your tile, then use this function:

$ailias=JFilterOutput::stringURLSafe($_POST['title']);

Now you can insert it into database.

Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453
nanhe
  • 41
  • 1
1

It's simple PHP.

Here is the function from Joomla 1.5 source:

Notice, I have commented the two lines out. You can call the function like

$new_alias = stringURLSafe($your_title);

function stringURLSafe($string)
    {
        //remove any '-' from the string they will be used as concatonater
        $str = str_replace('-', ' ', $string);
        $str = str_replace('_', ' ', $string);

        //$lang =& JFactory::getLanguage();
        //$str = $lang->transliterate($str);

        // remove any duplicate whitespace, and ensure all characters are alphanumeric
        $str = preg_replace(array('/\s+/','/[^A-Za-z0-9\-]/'), array('-',''), $str);

        // lowercase and trim
        $str = trim(strtolower($str));
        return $str;
    }
Paul van Jaarsveld
  • 1,524
  • 1
  • 10
  • 9
0

If anyone is looking for how to make an alias in Joomla 4, here is the solution:

use Joomla\CMS\Filter\OutputFilter;

(insert at the top of the document)

And then in the code.

$alias = OutputFilter::stringUrlSafe($string);