Skip to content
This repository was archived by the owner on Dec 9, 2025. It is now read-only.

string.inc

Tadatoshi Tokutake edited this page May 31, 2015 · 9 revisions

Index

Details

wrap

string wrap(string $string, string $wrapper)

  • Return the string wrapped by $wrapper.
  • Example:
var_dump(wrap('regex', '/')); // => /regex/

wrap_by_tag

string wrap_by_tag(string $string, string $tag, string $class = '')

  • Return the string wrapped by html tags.
  • Example:
var_dump(wrap_by_tag('hello, world!', 'p'        )); // => '<p>hello, world!</p>'
var_dump(wrap_by_tag('hello, world!', 'p', 'main')); // => '<p class="main">hello, world!</p>'

follow_join

string follow_join(string $glue, array $pieces)

  • Return implode($glue, $pieces) . $glue.
  • Example:
var_dump(follow_join(PHP_EOL, ['one', 'two', 'three']));
// => one
//    two
//    three

set_jp_encoding

void set_jp_encoding(string $encoding = 'UTF-8')

  • Set the character encoding of system and regular expression for Japanese.
  • Notice: This function allow 'ISO-8859-1', 'ASCII', 'JIS', 'UTF-8', 'EUC-JP' and 'SJIS' as the first argument.

start_with

bool start_with(string $haystack, string $needle)

  • Judge if $haystack starts with $needle.
  • Notice: If $needle is the empty string, This function always returns true.
  • Example:
$haystack = 'hello, world!';
var_dump(start_with($haystack, 'hello')); // => true
var_dump(start_with($haystack, 'world')); // => false

end_with

bool end_with(string $haystack, string $needle)

  • Judge if $haystack ends with $needle.
  • Notice: If $needle is the empty string, This function always returns true.
  • Example:
$haystack = 'hello, world!';
var_dump(end_with($haystack, 'world!')); // => true
var_dump(end_with($haystack, 'hello,')); // => false

have_any

bool have_any(string $haystack, array $needles)

  • Judge if $haystack includes any of $needles.
  • Example:
$haystack = 'hello, world!';
var_dump(match_any($haystack, ['hell' , 'heven'])); // => true
var_dump(match_any($haystack, ['devil', 'god'  ])); // => false

str_replace_first

string str_replace_first(string $search, string $replace, string $subject)

  • Return the string whose first matching part is replaced by $replace.
  • Example:
var_dump('hay', 'yo', 'hay! hay! hay!'); // => "yo! hay! hay!"

split_at

array split_at(string $string, int $at)

  • Return the pair of strings split at the position of $at.
  • Notice: If $at is negative, ifs absolute value specifies the position counted from the end.
  • Example:
var_dump('blur',  0); // => ["", "blur"]
var_dump('blur',  1); // => ["b", "lur"]
var_dump('blur', -1); // => ["blu", "r"]

mb_str_split

array mb_str_split(string $string, int $width = 1)

  • Return the array of strings split in $width.
  • Notice: $width must be positive.
  • Example:
var_dump('hello', 1); // => ["h", "e", "l", "l", "o"]
var_dump('hello', 2); // => ["he", "ll", "o"]

is_blank

bool is_blank(string $string)

  • Judge if $string consists of the blank characters.
  • Notice: Recommend to use this function under UTF-8.
  • Example:
var_dump("\t  \n"); // => true

mb_trim

string mb_trim(string $string)

  • Return the string removed the blank characters of the both ends.
  • Notice: Recommend to use this function under UTF-8.
  • Example:
var_dump(mb_trim("  echo 'hello';\n")); // => "echo 'hello';"

have_mb_char

bool have_mb_char(string $string)

  • Judge if $string has the multibyte character.
  • Example:
var_dump(have_mb_char('日本語' )); // => true
var_dump(have_mb_char('English')); // => false

Clone this wiki locally