Welcome Guest, Not a member yet? Register   Sign In
Use url_title() function and accept certain character
#1

[eluser]tolinho[/eluser]
Hi.
I'm woking on a small cms.

I and been using url_title function to convert the title of the page to the slug with no problems.

Now I'm working an a menu for my page and in my content manager I need to allow the slug to have "/" in it.
Because I would like to add an item (link) to my menu with the slug of (news/article/some-special-news)

Only that url_title removes all special characters, so I end up with (newsarticlesome-special-news) and a broken link

Can some please tell me how I can achieve this?
Or should I give this issue a different aproach?

Thanks!
#2

[eluser]ivantcholakov[/eluser]
In this case you may apply url_title() on each segment separately, and then join the segments with '/'.
#3

[eluser]tolinho[/eluser]
Hi.
That won't work in this case.
I have a input field for the slug were I add text or the slug.
In here is were I mite need to add a slug with "/" in it
Like this. (news/article/some-special-news)

Thanks for the suggestion :-)
#4

[eluser]ivantcholakov[/eluser]
Split the user input into an array of stings, the separator is '/'. Then on each string (each segment) apply url_title(). Then join the processed segments to get the result, again the glue is '/'. Nothing tricky is here
#5

[eluser]joergy[/eluser]
I guess You only need the last segment to be converted, don't You?
So simply use
url_title(basename("news/article/some-special-news"))
#6

[eluser]tolinho[/eluser]
[quote author="joergy" date="1403991830"]I guess You only need the last segment to be converted, don't You?
So simply use
url_title(basename("news/article/some-special-news"))[/quote]

Hi.
Not really. Its the entire slug like
(news/article/some-special-news)

So I can add new items to my menu.
I do this via input field were I add simple text or a link to an already existing page.
If i use only text all is fine.
If I need to link an page inside my site that in this case requires me to add "/"

Thanks
#7

[eluser]tolinho[/eluser]
[quote author="ivantcholakov" date="1403991317"]Split the user input into an array of stings, the separator is '/'. Then on each string (each segment) apply url_title(). Then join the processed segments to get the result, again the glue is '/'. Nothing tricky is here[/quote]

I'll give it a try.
I has hoping maybe passing a parameter with the character I need to allow would do the trick instead or just of just removing all special characters like url_title does.

Thanks
#8

[eluser]tolinho[/eluser]
Hi.
So I made a function to use instead on url_title for the cases I need "/" in the slug, and maybe some other caracters
Based on information found at:
http://stackoverflow.com/questions/14114...m-a-string

Code:
if ( ! function_exists('process_slug'))
{
    function process_slug($string, $low = TRUE)
    {        
        $string = strip_tags($string);
        $string = preg_replace('/  */', '-', $string); // Replace sequences of spaces with hyphen
        $string = preg_replace('/[^A-Za-z0-9_#~&?=\\\.\/\-]/', '', $string); // Removes some special chars leaves _ # ~ & ? = \ . /
        if ($low) $string = strtolower($string);
        return $string;
    }    
}

Its working okay for me, it mite work for you.
It should be safe has i'm using it in the rules like this:
Code:
'rules' => 'trim|required|max_length[100]|convert_accented_characters|process_slug|xss_clean'

If anyone seems improvements that can be made please point them out.




Theme © iAndrew 2016 - Forum software by © MyBB