-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
168614c
commit d274a9a
Showing
3 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
namespace Stringizer\Transformers; | ||
|
||
use Stringizer\Stringizer; | ||
|
||
/** | ||
* Between - Extracts a string between left and right strings. | ||
* | ||
* @link https://github.com/jasonlam604/Stringizer | ||
* @copyright Copyright (c) 2016 Jason Lam | ||
* @license https://github.com/jasonlam604/Stringizer/blob/master/LICENSE (MIT License) | ||
*/ | ||
class Between extends Transformer implements TransformerInterface | ||
{ | ||
|
||
private $left; | ||
private $right; | ||
|
||
public function __construct($value, $left, $right) | ||
{ | ||
parent::__construct($value); | ||
$this->left = $left; | ||
$this->right = $right; | ||
} | ||
|
||
public function execute() | ||
{ | ||
$str = $this->getValue(); | ||
|
||
if (mb_substr($this->getValue(), 0, mb_strlen($this->left)) == $this->left) { | ||
$str = mb_substr($this->getValue(), mb_strlen($this->left)); | ||
} | ||
|
||
if(mb_substr($this->getValue(), mb_strlen($this->getValue()) - mb_strlen($this->right), mb_strlen($this->right)) == $this->right) { | ||
$str = mb_substr($str, 0, mb_strlen($str) - mb_strlen($this->right)); | ||
} | ||
|
||
return $str; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
use Stringizer\Stringizer; | ||
|
||
/** | ||
* Between Unit Tests | ||
*/ | ||
class BetweenTest extends PHPUnit_Framework_TestCase | ||
{ | ||
public function testValid() | ||
{ | ||
$s = new Stringizer("<div>Hello World</div>"); | ||
$this->assertEquals("Hello World", $s->between("<div>", "</div>")); | ||
|
||
// Right Side no match, retains the last right side </div> | ||
$s = new Stringizer("<div>Hello World</div></div>"); | ||
$this->assertEquals("Hello World</div>", $s->between("<div>", "</div>")); | ||
|
||
$s = new Stringizer("<div>Hello World"); | ||
$this->assertEquals("Hello World", $s->between("<div>", "</div>")); | ||
} | ||
|
||
public function testValidMultiByte() | ||
{ | ||
$s = new Stringizer("<div>ȘŦŗÍñĝìzĕŕ</div>"); | ||
$this->assertEquals("ȘŦŗÍñĝìzĕŕ", $s->between("<div>", "</div>")); | ||
|
||
$s = new Stringizer("<div>こんにちは</div>"); | ||
$this->assertEquals("こんにちは", $s->between("<div>", "</div>")); | ||
} | ||
|
||
} |