Last active
May 8, 2016 23:26
-
-
Save s-hiroshi/6cf2067175b29a020350c8a66a777e12 to your computer and use it in GitHub Desktop.
WordPressの文字列に関する汎用処理をまとめたユーティリティークラスです。
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
<?php | |
/** | |
* InfoTown_Utils_Strテストクラス | |
* | |
* @package InfoTown | |
* @subpackage tests | |
* @author Hiroshi Sawai <info@info-town.jp> | |
* @copyright Hiroshi Sawai | |
* @version 1.0.0 | |
* @since 1.0.0 | |
*/ | |
class InfoTown_Utils_Str_Test extends WP_UnitTestCase { | |
public static function setUpBeforeClass() { | |
parent::setUpBeforeClass(); | |
require_once( dirname( dirname( dirname( __FILE__ ) ) ) . '/inc/utils/class.date.php' ); | |
} | |
/** | |
* @group str | |
*/ | |
public function test_trim() { | |
$actual = InfoTown_Utils_Str::trim( '本日は晴天なり。', | |
5, | |
[ | |
'prefix' => '「', | |
'suffix' => '」', | |
] ); | |
$this->assertEquals( '「本日は晴天」', $actual ); | |
} | |
/** | |
* @group str | |
*/ | |
public function test_get_the_excerpt() { | |
$post_id = $this->factory->post->create( [ | |
'post_excerpt' => '本日は晴天なり。' | |
] ); | |
$this->go_to( "/?p=" . $post_id ); | |
$actual = InfoTown_Utils_Str::get_the_excerpt( 5 ); | |
$this->assertEquals( '本日は晴天', $actual ); | |
} | |
} |
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
<?php | |
/** | |
* 文字処理 | |
* | |
* @package InfoTown | |
* @author Hiroshi Sawai <info@info-town.jp> | |
* @copyright Hiroshi Sawai | |
*/ | |
class InfoTown_Utils_Str { | |
/** | |
* 文字列切り取り | |
* | |
* 引数のDateTimeオジェジュクトは現在時刻から$diffで渡した期間を経過しているか検証します。 | |
* | |
* @param string $str 切り取り対象文字列です。 | |
* @param string $length 切り取り文字数です。 | |
* @param array $options prefixとsuffixです。 | |
* prefix プレフィックスです。 | |
* suffix サフィックスです。 | |
* | |
* @return string 切り取り文字へprefixとsuffixを付加した文字列です。 | |
*/ | |
public static function trim( $str, $length, $options = array() ) { | |
$str = mb_substr( $str, 0, $length ); | |
if ( isset( $options['prefix'] ) && '' !== $options['prefix'] ) { | |
$str = $options['prefix'] . $str; | |
} | |
if ( isset( $options['suffix'] ) && '' !== $options['suffix'] ) { | |
$str .= $options['suffix']; | |
} | |
return $str; | |
} | |
/** | |
* 概要から指定文字数取得 | |
* | |
* @param integer $length 表示文字数です。 | |
* | |
* @return number excerpt length | |
*/ | |
public static function get_the_excerpt( $length ) { | |
$data = get_the_excerpt(); | |
$data = mb_substr( $data, 0, $length, 'UTF-8' ); | |
return $data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment