If you have a blog in which you’d like to use the title of your entries in the URL, you need to convert them to URL slugs. CodeIgniter’s built-in library offers a method to help us with this task.
URL Helper is a library that assists in working with URLs. You can use this helper’s methods to retrieve data or format URLs such as getting the application’s base URL/domain and printing a link with an anchor.
In this post, we will talk about the url_title()
method, which takes a string as input and creates a human-friendly URL string.
url_title($str, $separator = '-', $lowercase = FALSE)
$str
: The input string that you want to convert to a URL slug.$separator
: The character to use as a separator between words in the URL slug. By default, this is a hyphen (-).$lowercase
: A boolean value that determines whether to convert the URL slug to lowercase. By default, this is set to FALSE.
$title = "Convert title to URL slug in CodeIgniter"; $url_title = url_title($title, '-', true); //output: convert-title-to-url-slug-in-codeigniter $title = "This is a Sample Title"; $slug = url_title($title, '_', TRUE); echo $slug; //output: this_is_a_sample_title