mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
Closes #707, added Inflector::slug() to return a string with all spaces converted to $replacement and non word characters removed.
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5712 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
parent
c7d2c7bacd
commit
9a4d314b81
2 changed files with 27 additions and 1 deletions
|
@ -411,8 +411,20 @@ class Inflector extends Object {
|
|||
$variable = preg_replace('/\\w/', $replace, $string, 1);
|
||||
return $variable;
|
||||
}
|
||||
/**
|
||||
* Returns a string with all spaces converted to $replacement and non word characters removed.
|
||||
*
|
||||
* @param string $string
|
||||
* @param string $replacement
|
||||
* @return string
|
||||
* @access public
|
||||
* @static
|
||||
*/
|
||||
function slug($string, $replacement = '_') {
|
||||
$string = preg_replace(array('/[^\w\s]/', '/\\s+/') , array(' ', $replacement), $string);
|
||||
return $string;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enclose a string for preg matching.
|
||||
*
|
||||
|
|
|
@ -173,6 +173,20 @@ class InflectorTest extends UnitTestCase {
|
|||
$this->assertEqual($result, $expected);
|
||||
}
|
||||
|
||||
function testInflectorSlug() {
|
||||
$result = $this->Inflector->slug('Foo Bar: Not just for breakfast any-more');
|
||||
$expected = 'Foo_Bar_Not_just_for_breakfast_any_more';
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->Inflector->slug('Foo Bar: Not just for breakfast any-more', "-");
|
||||
$expected = 'Foo-Bar-Not-just-for-breakfast-any-more';
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->Inflector->slug('Foo Bar: Not just for breakfast any-more', "+");
|
||||
$expected = 'Foo+Bar+Not+just+for+breakfast+any+more';
|
||||
$this->assertEqual($result, $expected);
|
||||
}
|
||||
|
||||
function tearDown() {
|
||||
unset($this->Inflector);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue