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

Add mb_url_title() function #3086

Merged
merged 1 commit into from
Jun 11, 2020
Merged
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
24 changes: 24 additions & 0 deletions system/Helpers/url_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -613,4 +613,28 @@ 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
*
* Takes a "title" string as input and creates a
* human-friendly URL string with a "separator" string
* as the word separator.
*
* @param string $str Input string
* @param string $separator Word separator (usually '-' or '_')
* @param boolean $lowercase Whether to transform the output string to lowercase
* @return string
*/
function mb_url_title(string $str, string $separator = '-', bool $lowercase = false): string
{
helper('text');

return url_title(convert_accented_characters($str), $separator, $lowercase);
}
}

//--------------------------------------------------------------------
39 changes: 38 additions & 1 deletion tests/system/Helpers/URLHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ protected function setUp(): void
parent::setUp();

helper('url');
Services::reset();
Services::reset(true);
}

public function tearDown(): void
Expand Down Expand Up @@ -1142,6 +1142,43 @@ public function testUrlTitleExtraDashes()
}
}

//--------------------------------------------------------------------
// Test mb_url_title

public function testMbUrlTitle()
{
helper('text');

$words = [
'foo bar /' => 'foo-bar',
'\ testing 12' => 'testing-12',
'Éléphant de PHP' => 'elephant-de-php',
'ä ö ü Ĝ β ę' => 'ae-oe-ue-g-v-e',
];

foreach ($words as $in => $out)
{
$this->assertEquals($out, mb_url_title($in, '-', true));
}
}

public function testMbUrlTitleExtraDashes()
{
helper('text');

$words = [
'_foo bar_' => 'foo_bar',
'_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)
{
$this->assertEquals($out, mb_url_title($in, '_'));
}
}

//--------------------------------------------------------------------
// Exploratory testing, investigating https://github.com/codeigniter4/CodeIgniter4/issues/2016

Expand Down
11 changes: 11 additions & 0 deletions user_guide_src/source/helpers/url_helper.rst
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,17 @@ The following functions are available:
$url_title = url_title($title, '-', TRUE);
// Produces: whats-wrong-with-css

.. php:function:: mb_url_title($str[, $separator = '-'[, $lowercase = FALSE]])

:param string $str: Input string
:param string $separator: Word separator (usually '-' or '_')
:param bool $lowercase: Whether to transform the output string to lowercase
:returns: URL-formatted string
:rtype: string

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

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

:param string $str: URL string
Expand Down