Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements to the mb_url_title() helper #3185

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions system/Helpers/url_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ function url_title(string $str, string $separator = '-', bool $lowercase = false
if (! function_exists('mb_url_title'))
{
/**
* Create URL Title that takes into account accented characters
* Create URL Title that takes into account Unicode and accented characters
*
* Takes a "title" string as input and creates a
* human-friendly URL string with a "separator" string
Expand All @@ -633,7 +633,26 @@ function mb_url_title(string $str, string $separator = '-', bool $lowercase = fa
{
helper('text');

return url_title(convert_accented_characters($str), $separator, $lowercase);
$q_separator = preg_quote($separator, '#');

$trans = [
'[^\pL\pM\d _-]' => '',
'\s+' => $separator,
'(' . $q_separator . ')+' => $separator,
];

$str = strip_tags(convert_accented_characters($str));
foreach ($trans as $key => $val)
{
$str = preg_replace('#' . $key . '#iu', $val, $str);
}

if ($lowercase === true)
{
$str = mb_strtolower($str);
}

return trim(trim($str, $separator));
}
}

Expand Down
2 changes: 2 additions & 0 deletions tests/system/Helpers/URLHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1154,6 +1154,7 @@ public function testMbUrlTitle()
'\ testing 12' => 'testing-12',
'Éléphant de PHP' => 'elephant-de-php',
'ä ö ü Ĝ β ę' => 'ae-oe-ue-g-v-e',
'মশিউর রহমান' => 'মশিউর-রহমান',
];

foreach ($words as $in => $out)
Expand All @@ -1171,6 +1172,7 @@ public function testMbUrlTitleExtraDashes()
'_What\'s wrong with CSS?_' => 'Whats_wrong_with_CSS',
'Éléphant de PHP' => 'Elephant_de_PHP',
'ä ö ü Ĝ β ę' => 'ae_oe_ue_G_v_e',
'মশিউর রহমান' => 'মশিউর_রহমান',
];

foreach ($words as $in => $out)
Expand Down
4 changes: 2 additions & 2 deletions user_guide_src/source/helpers/url_helper.rst
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,8 @@ The following functions are available:
:returns: URL-formatted string
:rtype: string

This function works the same as :php:func:`url_title()` but it converts all
accented characters automatically.
This function works the same as :php:func:`url_title()` but it supports Unicode
and converts all accented characters automatically.

.. php:function:: prep_url($str = '')

Expand Down