I'm wondering if anyone else here would find it useful to have an optional word separator for fURL::makeFriendly() as a third parameter, with the default remaining underscore. While I prefer underscore, some people seem to prefer dashes as word separators.
I think this would be a great addition, I've taken the easy way out and done the following a few times:
str_replace('_', '-', fURL::makeFriendly($uglyUrl));
I think it would be a great addition, one I would personally use, here is a patch
@@ -92,21 +92,22 @@
*
* @param string $string The string to convert
* @param interger $max_length The maximum length of the friendly URL
+ * @param string $separator Character to use between words in URL, defaults to '_'
* @return string The URL-friendly version of the string
*/
- static public function makeFriendly($string, $max_length=NULL)
+ static public function makeFriendly($string, $max_length=NULL, $separator='_')
{
+ $regexSeparator = preg_quote($separator);
$string = fHTML::decode(fUTF8::ascii($string));
$string = strtolower(trim($string));
$string = str_replace("'", '', $string);
- $string = preg_replace('#[^a-z0-9\\-]+#', '_', $string);
- $string = preg_replace('#_{2,}#', '_', $string);
- $string = preg_replace('#_-_#', '-', $string);
- $string = preg_replace('#(^_+|_+$)#D', '', $string);
+ $string = preg_replace('#[^a-z0-9\\-]+#', $separator, $string);
+ $string = preg_replace('#' . $regexSeparator . '{2,}#', $separator, $string);
+ $string = preg_replace('#(^' . $regexSeparator . '+|' . $regexSeparator . '+$)#D', '', $string);
$length = strlen($string);
if ($max_length && $length > $max_length) {
- $last_pos = strrpos($string, '_', ($length - $max_length - 1) * -1);
+ $last_pos = strrpos($string, $separator, ($length - $max_length - (strlen($separator)) * -1);
if ($last_pos < ceil($max_length / 2)) {
$last_pos = $max_length;
}
Your patch is pretty much exactly what I did on my fork where I kinda "needed" the functionality. https://github.com/dotink/flourish/commit/922930d1e7568b5dfbc37ddec7d4dd1b2d170113 -- Although there is one thing I am a bit confused about and that is namely the use of preg_quote. I would understand it for certain characters, and obviously it's good to have in there in the event the user does put something that need to be escaped for some reason (although I can't imagine this going behind _ and -, but despite it being listed in preg_quote man page, it doesn't seem to be required for a dash. As you can see from my above link, I don't use it in mine and this seems to work fine:
[/home/gent/Projects/inkwell.dotink.org/includes][01]# echo fURL::makeFriendly('This is a test of the separator', NULL, '-');
this-is-a-test-of-the-separator
[/home/gent/Projects/inkwell.dotink.org/includes][01]#
Yea, didn't need it for the dash, I just put it in there in case someone were to pass something to the function that did require regex escaping. Not being able to control the caller of the function, it was just a bit of future proofing.
Yeah, I've gone ahead an added that in, although it's probably not very SEO friendly I personally like a '.'
[/home/gent/Projects/inkwell.dotink.org/includes][01]# echo fURL::makeFriendly('This is a test of the separator', NULL, '.');
this.is.a.test.of.the.separator
[/home/gent/Projects/inkwell.dotink.org/includes][01]#
Obviously that would have some issue without it!
As of r1004 there is now a $delimiter
parameter for fURL::makeFriendly() that should theoretically accept any non-empty string, although I imagine it will normally be set to -
.
fURL::makeFriendly('This is a test of the separator', '-');
// With length
fURL::makeFriendly('This is a test of the separater', 10, '-');