phpで文字列から最初の5文字を取得する方法
$myStr = "HelloWordl";
結果は次のようになります。
$result = "Hello";
シングルバイト文字列](http://en.wikipedia.org/wiki/Single-byte_encoding) (US-ASCII, ISO 8859ファミリーなど)にはsubstrを,マルチバイト文字列 (UTF-8, UTF-16など)にはmb_substrを使用してください。
substr
mb_substr
// singlebyte strings $result = substr($myStr, 0, 5); // multibyte strings $result = mb_substr($myStr, 0, 5);
substr()`](http://www.php.net/manual/en/function.substr.php)をご利用ください。
$result = substr($myStr, 0, 5);
このようにsubstr`関数を使うことができます。
echo substr($myStr, 0, 5);
substr`の第2引数はどの位置から始めたいか、第3引数は何文字で返したいかです。
シングルバイト文字列](http://en.wikipedia.org/wiki/Single-byte_encoding) (US-ASCII, ISO 8859ファミリーなど)には
substr
を,マルチバイト文字列 (UTF-8, UTF-16など)にはmb_substr
を使用してください。substr()`](http://www.php.net/manual/en/function.substr.php)をご利用ください。
このようにsubstr`関数を使うことができます。
substr`の第2引数はどの位置から始めたいか、第3引数は何文字で返したいかです。