1
0
mirror of https://github.com/danielstjules/Stringy.git synced 2025-09-01 17:12:42 +02:00

7 Commits
1.2.1 ... 1.2.2

Author SHA1 Message Date
Daniel St. Jules
6ba29637b2 Updated changelog for 1.2.2 2013-12-04 23:36:17 -05:00
Daniel St. Jules
2ada00ff62 Add logo 2013-12-01 15:43:03 -05:00
Daniel St. Jules
2a197244a5 Update readme and comments for new slugify param 2013-11-27 01:14:09 -05:00
Daniel St. Jules
8b1a91fe0c Merge pull request #15 from glynnforrest/slug
Adding optional $replacement parameter to slugify.
2013-11-26 22:03:02 -08:00
Glynn Forrest
906f217dc1 Adding optional $replacement parameter to slugify. 2013-11-26 14:11:41 +00:00
Daniel St. Jules
d4baab7583 Merge pull request #14 from sergebezborodov/master
Use late static bindings in Stringy::create
2013-11-05 20:00:18 -08:00
Serge Bezborodov
0c8f0e9083 changes in create function 2013-11-01 09:55:38 +02:00
7 changed files with 33 additions and 21 deletions

View File

@@ -1,3 +1,8 @@
### 1.2.2 (2013-12-04)
* Updated create function to use late static binding
* Added optional $replacement param to slugify
### 1.2.1 (2013-10-11)
* Cleaned up tests

View File

@@ -1,4 +1,4 @@
# Stringy
![Stringy](http://danielstjules.com/stringy/logo.png)
A PHP library with a variety of string manipulation functions with multibyte support. Offers both OO method chaining and a procedural-style static wrapper. Compatible with PHP 5.3+. Inspired by underscore.string.js.
@@ -648,14 +648,15 @@ S::shuffle('fòô bàř', 'UTF-8'); // 'àôřb òf'
#### slugify
$stringy->slugify()
$stringy->slugify([ string $replacement = '-' ])
S::slugify(string $str)
S::slugify(string $str [, string $replacement = '-' ])
Converts the string into an URL slug. This includes replacing non-ASCII
characters with their closest ASCII equivalents, removing non-alphanumeric
and non-ASCII characters, and replacing whitespace with dashes. The string
is also converted to lowercase.
and non-ASCII characters, and replacing whitespace with $replacement.
The replacement defaults to a single dash, and the string is also
converted to lowercase.
```php
S::create('Using strings like fòô bàř')->slugify();

View File

@@ -325,15 +325,17 @@ class StaticStringy
/**
* Converts the string into an URL slug. This includes replacing non-ASCII
* characters with their closest ASCII equivalents, removing non-alphanumeric
* and non-ASCII characters, and replacing whitespace with dashes. The string
* is also converted to lowercase.
* and non-ASCII characters, and replacing whitespace with $replacement.
* The replacement defaults to a single dash, and the string is also
* converted to lowercase.
*
* @param string $str Text to transform into an URL slug
* @param string $str Text to transform into an URL slug
* @param string $replacement The string used to replace whitespace
* @return string The corresponding URL slug
*/
public static function slugify($str)
public static function slugify($str, $replacement = '-')
{
return Stringy::create($str)->slugify()->str;
return Stringy::create($str)->slugify($replacement)->str;
}
/**

View File

@@ -33,7 +33,7 @@ class Stringy
*/
public static function create($str, $encoding = null)
{
return new self($str, $encoding);
return new static($str, $encoding);
}
/**
@@ -577,18 +577,20 @@ class Stringy
/**
* Converts the string into an URL slug. This includes replacing non-ASCII
* characters with their closest ASCII equivalents, removing non-alphanumeric
* and non-ASCII characters, and replacing whitespace with dashes. The string
* is also converted to lowercase.
* and non-ASCII characters, and replacing whitespace with $replacement.
* The replacement defaults to a single dash, and the string is also
* converted to lowercase.
*
* @param string $replacement The string used to replace whitespace
* @return Stringy Object whose $str has been converted to an URL slug
*/
public function slugify()
public function slugify($replacement = '-')
{
$stringy = self::create($this->str, $this->encoding);
$stringy->str = preg_replace('/[^a-zA-Z\d -]/u', '', $stringy->toAscii());
$stringy->str = preg_replace("/[^a-zA-Z\d $replacement]/u", '', $stringy->toAscii());
$stringy->str = $stringy->collapseWhitespace()->str;
$stringy->str = str_replace(' ', '-', strtolower($stringy->str));
$stringy->str = str_replace(' ', $replacement, strtolower($stringy->str));
return $stringy;
}

View File

@@ -330,7 +330,9 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
array('unrecognized-chars-like', 'unrecognized chars like συγγρ'),
array('numbers-1234', 'numbers 1234'),
array('perevirka-ryadka', 'перевірка рядка'),
array('bukvar-s-bukvoy-y', 'букварь с буквой ы')
array('bukvar-s-bukvoy-y', 'букварь с буквой ы'),
array('foo:bar:baz', 'Foo bar baz', ':'),
array('a_string_with_underscores', 'A_string with_underscores', '_')
);
}

View File

@@ -245,9 +245,9 @@ class StaticStringyTestCase extends CommonTest
/**
* @dataProvider slugifyProvider()
*/
public function testSlugify($expected, $str)
public function testSlugify($expected, $str, $replacement = '-')
{
$result = S::slugify($str);
$result = S::slugify($str, $replacement);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}

View File

@@ -312,10 +312,10 @@ class StringyTestCase extends CommonTest
/**
* @dataProvider slugifyProvider()
*/
public function testSlugify($expected, $str)
public function testSlugify($expected, $str, $replacement = '-')
{
$stringy = S::create($str);
$result = $stringy->slugify();
$result = $stringy->slugify($replacement);
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);