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

Adding commonly used slugifying support to illuminate/support. #307

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
116 changes: 116 additions & 0 deletions src/Illuminate/Support/Slugifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php namespace Illuminate\Support;

class Slugifier {

/**
* Array of foreign characters and their 7-bit ASCII equivalents.
*
* @var array
*/
protected static $ascii = array(
'/æ|ǽ/' => 'ae',
'/œ/' => 'oe',
'/À|Á|Â|Ã|Ä|Å|Ǻ|Ā|Ă|Ą|Ǎ|А/' => 'A',
'/à|á|â|ã|ä|å|ǻ|ā|ă|ą|ǎ|ª|а/' => 'a',
'/Б/' => 'B',
'/б/' => 'b',
'/Ç|Ć|Ĉ|Ċ|Č|Ц/' => 'C',
'/ç|ć|ĉ|ċ|č|ц/' => 'c',
'/Ð|Ď|Đ|Д/' => 'Dj',
'/ð|ď|đ|д/' => 'dj',
'/È|É|Ê|Ë|Ē|Ĕ|Ė|Ę|Ě|Е|Ё|Э/' => 'E',
'/è|é|ê|ë|ē|ĕ|ė|ę|ě|е|ё|э/' => 'e',
'/Ф/' => 'F',
'/ƒ|ф/' => 'f',
'/Ĝ|Ğ|Ġ|Ģ|Г/' => 'G',
'/ĝ|ğ|ġ|ģ|г/' => 'g',
'/Ĥ|Ħ|Х/' => 'H',
'/ĥ|ħ|х/' => 'h',
'/Ì|Í|Î|Ï|Ĩ|Ī|Ĭ|Ǐ|Į|İ|И/' => 'I',
'/ì|í|î|ï|ĩ|ī|ĭ|ǐ|į|ı|и/' => 'i',
'/Ĵ|Й/' => 'J',
'/ĵ|й/' => 'j',
'/Ķ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Zh',
'/ж/' => 'zh',
);

/**
* Array of replacements to be made when creating
* a slug value.
*
* @var array
*/
protected static $replacements = array(
'/[^\x09\x0A\x0D\x20-\x7E]/' => '',
);

/**
* Creates a slug from the given string.
*
* @param string $value
* @param string $separator
* @return string
*/
public static function slug($value, $separator = '-')
{
// Firstly, we'll do a big replacement to strip out foreign keys
// along with any custom replacements required to create a slug.
$value = preg_replace(
array_merge(array_keys(static::$ascii), array_keys(static::$replacements)),
array_merge(array_values(static::$ascii), array_values(static::$replacements)),
$value
);

// Remove all characters that are not the separator, letters, numbers, or whitespace.
$value = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', strtolower($value));

// Replace all separator characters and whitespace by a single separator
$value = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $value);

return trim($value, $separator);
}

}
15 changes: 13 additions & 2 deletions src/Illuminate/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,17 @@ function str_plural($value, $count = 2)
return Illuminate\Support\Pluralizer::plural($value, $count);
}

/**
* Returns a slug representation of a string.
*
* @param string $value
* @param string $separator
*/
function str_slug($value, $separator = '-')
{
return Illuminate\Support\Slugifier::slug($value, $separator);
}

/**
* Generate a "random" alpha-numeric string.
*
Expand All @@ -491,7 +502,7 @@ function str_random($length = 16)
{
$pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

return substr(str_shuffle(str_repeat($pool, 5)), 0, $length);
return substr(str_shuffle(str_repeat($pool, 5)), 0, $length);
}

/**
Expand Down Expand Up @@ -560,4 +571,4 @@ function with($object)
return $object;
}

}
}
2 changes: 1 addition & 1 deletion tests/Support/SupportHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,4 @@ public function testValue()
$this->assertEquals('foo', value(function() { return 'foo'; }));
}

}
}
2 changes: 1 addition & 1 deletion tests/Support/SupportPluralizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ public function testBasicUsage()
$this->assertEquals('deer', str_singular('deer'));
}

}
}
12 changes: 12 additions & 0 deletions tests/Support/SupportSlugifierTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

class SupportSlugifierTest extends PHPUnit_Framework_TestCase {

public function testBasicUsage()
{
$this->assertEquals('this-is-my-blog-post', str_slug('This is my blog post!'));
$this->assertEquals('voila-success', str_slug('Voilà, Success! '));
$this->assertEquals('boy_bear', str_slug(' Boy & Bear. ', '_'));
}

}