もう迷わない! PHP エンコーディングと iconv_strrpos 関数:サンプルコードでわかりやすく解説
基本的な構文
iconv_strrpos(haystack, needle, encoding);
引数
- encoding: 文字列のエンコーディング (省略可能。省略した場合、
iconv.internal_encoding
が使用されます) - needle: 検索するサブストリング
- haystack: 検索対象となる文字列
戻り値
- needle が見つからない場合、FALSE を返します。
- needle が haystack 内に見つかった場合、needle が最後に現れる位置 (文字数) を返します。
例
$haystack = "This is an example string";
$needle = "example";
$encoding = "UTF-8";
$position = iconv_strrpos($haystack, $needle, $encoding);
if ($position !== FALSE) {
echo "The last occurrence of \"$needle\" is at position: " . $position . "\n";
} else {
echo "The substring \"$needle\" was not found.\n";
}
The last occurrence of "example" is at position: 19
- encoding パラメータは省略可能ですが、異なるエンコーディング で文字列を処理する場合には、明示的に指定することをお勧めします。
- haystack または needle が文字列ではない場合、iconv_strrpos 関数 はその値を文字に変換してから処理します。
- iconv_strrpos 関数 は、バイト単位 ではなく 文字単位 で位置を返します。
例 1: UTF-8 エンコーディングで文字列を検索
この例では、UTF-8 エンコーディング でエンコードされた文字列 "This is an example string" 内の "example" サブストリングの最後の出現位置を検索します。
<?php
$haystack = "This is an example string";
$needle = "example";
$encoding = "UTF-8";
$position = iconv_strrpos($haystack, $needle, $encoding);
if ($position !== FALSE) {
echo "The last occurrence of \"$needle\" is at position: " . $position . "\n";
} else {
echo "The substring \"$needle\" was not found.\n";
}
出力
The last occurrence of "example" is at position: 19
例 2: Shift-JIS エンコーディングで文字列を検索
この例では、Shift-JIS エンコーディング でエンコードされた文字列 "これは例です" 内の "例" サブストリングの最後の出現位置を検索します。
<?php
$haystack = "これは例です";
$needle = "例";
$encoding = "Shift-JIS";
$position = iconv_strrpos($haystack, $needle, $encoding);
if ($position !== FALSE) {
echo "The last occurrence of \"$needle\" is at position: " . $position . "\n";
} else {
echo "The substring \"$needle\" was not found.\n";
}
出力
The last occurrence of "例" is at position: 5
例 3: 文字列にエンコーディングを指定しない
この例では、iconv.internal_encoding ディレクティブで設定されたエンコーディングを使用して、"This is an example string" 文字列内の "example" サブストリングの最後の出現位置を検索します。
<?php
$haystack = "This is an example string";
$needle = "example";
// iconv.internal_encoding ディレクティブを UTF-8 に設定
ini_set('iconv.internal_encoding', 'UTF-8');
$position = iconv_strrpos($haystack, $needle);
if ($position !== FALSE) {
echo "The last occurrence of \"$needle\" is at position: " . $position . "\n";
} else {
echo "The substring \"$needle\" was not found.\n";
}
出力
The last occurrence of "example" is at position: 19
例 4: mb_strrpos 関数と比較
この例では、mb_strrpos 関数 と iconv_strrpos 関数 を使用して、UTF-8 エンコーディング でエンコードされた文字列 "This is an example string" 内の "example" サブストリングの最後の出現位置を検索します。
<?php
$haystack = "This is an example string";
$needle = "example";
$encoding = "UTF-8";
// mb_strrpos 関数を使用して検索
$mb_position = mb_strrpos($haystack, $needle, $encoding);
if ($mb_position !== FALSE) {
echo "mb_strrpos: The last occurrence of \"$needle\" is at position: " . $mb_position . "\n";
} else {
echo "mb_strrpos: The substring \"$needle\" was not found.\n";
}
// iconv_strrpos 関数を使用して検索
$iconv_position = iconv_strrpos($haystack, $needle, $encoding);
if ($iconv_position !== FALSE) {
echo "iconv_strrpos: The last occurrence of \"$needle\" is at position: " . $iconv_position . "\n";
} else {
echo "iconv_strrpos: The substring \"$needle\" was not found.\n";
}
mb_strrpos: The last occurrence of "example" is at position: 19
iconv_strrpos: The last occurrence of "example" is at position:
mb_strrpos 関数
mb_strrpos 関数は、iconv_strrpos 関数とほぼ同じ機能を提供しますが、マルチバイト文字 をより効率的に処理するように設計されています。iconv_strrpos 関数を使用する代わりに、mb_strrpos 関数を使用することを 強くお勧め します。
例
$haystack = "This is an example string";
$needle = "example";
$encoding = "UTF-8";
$position = mb_strrpos($haystack, $needle, $encoding);
if ($position !== FALSE) {
echo "The last occurrence of \"$needle\" is at position: " . $position . "\n";
} else {
echo "The substring \"$needle\" was not found.\n";
}
strrpos 関数
strrpos 関数は、iconv_strrpos 関数 と mb_strrpos 関数 と異なり、エンコーディング を考慮しません。ASCII 文字 のみを含む文字列を検索する場合、strrpos 関数 は iconv_strrpos 関数 や mb_strrpos 関数 よりも高速で効率的です。
例
$haystack = "This is an example string";
$needle = "example";
$position = strrpos($haystack, $needle);
if ($position !== FALSE) {
echo "The last occurrence of \"$needle\" is at position: " . $position . "\n";
} else {
echo "The substring \"$needle\" was not found.\n";
}
正規表現
正規表現 を使用して、iconv_strrpos 関数 や mb_strrpos 関数 と同等の機能を実現することもできます。正規表現は、より複雑な検索パターンを処理するのに役立ちますが、iconv_strrpos 関数 や mb_strrpos 関数 よりも処理速度が遅くなる場合があります。
例
$haystack = "This is an example string";
$needle = "example";
$encoding = "UTF-8";
$pattern = "/$needle$/" . $encoding;
$position = preg_match_all($pattern, $haystack, $matches);
if ($position > 0) {
echo "The last occurrence of \"$needle\" is at position: " . $matches[0][0] . "\n";
} else {
echo "The substring \"$needle\" was not found.\n";
}
手動ループ
iconv_strrpos 関数 や mb_strrpos 関数 を使用せずに、手動ループ で文字列を検索することもできます。これは、パフォーマンス が重要ではない場合や、iconv_strrpos 関数 や mb_strrpos 関数 が利用できない場合に役立ちます。
$haystack = "This is an example string";
$needle = "example";
$encoding = "UTF-8";
$position = -1;
for ($i = strlen($haystack) - strlen($needle); $i >= 0; $i--) {
if (substr($haystack, $i, strlen($needle)) === $needle) {
$position = $i;
break;
}
}
if ($position !== -1) {
echo "The last occurrence of \"$needle\" is at position: " . $position . "\n";
} else {
echo "The substring \"$needle\" was not found.\n";
}