CodeIgniter Forums
Trying to make a url generator from a title - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Trying to make a url generator from a title (/showthread.php?tid=69610)



Trying to make a url generator from a title - doomie22 - 12-21-2017

Hi there,

I have been trying to make a generator that will take a title and strip out all the special characters like !"£$ etc...

I have used strtolower and str_replace in a chain with each character but it is inconsistent and will not strip everything like ~.

Is there anything out there that can help with something like this,  I have had a bit of a search but nothing had turned up yet.

Thanks,

Doomie


RE: Trying to make a url generator from a title - ragingTorch - 12-21-2017

Try using the url_title from the url helper: https://www.codeigniter.com/user_guide/helpers/url_helper.html#url_title


RE: Trying to make a url generator from a title - nickimnick - 12-22-2017

Hi,
I don't know which version Codeigniter you are using but if you're using 3.x, you can try URL helper function: url_title. You can reach the references with the link below.

https://www.codeigniter.com/user_guide/helpers/url_helper.html#url_title

Hope this helps


RE: Trying to make a url generator from a title - XtreemDeveloper - 12-23-2017

First of all we need to strip all special characters and punctuation away. 
This is easily accomplished with something like:

function toAscii($str) {
$clean = preg_replace("/[^a-zA-Z0-9/_|+ -]/", '', $str);
$clean = strtolower(trim($clean, '-'));
$clean = preg_replace("/[/_|+ -]+/", '-', $clean);

return $clean;
}

Second option
setlocale(LC_ALL, 'en_US.UTF8');
function toAscii($str) {
$clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
$clean = preg_replace("/[^a-zA-Z0-9/_| -]/", '', $clean);
$clean = strtolower(trim($clean, '-'));
$clean = preg_replace("/[/_| -]+/", '-', $clean);

return $clean;
}