「PHPの基礎 - 文字列」の版間の差分

ナビゲーションに移動 検索に移動
300行目: 300行目:
  </syntaxhighlight>
  </syntaxhighlight>
<br>
<br>
==== 大文字・小文字を区別しない検索の実装例 ====
==== 大文字・小文字を区別しない検索 ====
  <syntaxhighlight lang="php">
  <syntaxhighlight lang="php">
  function mb_stripos_custom($haystack, $needle)
  function mb_stripos_custom($haystack, $needle)
313行目: 313行目:
  $position = mb_stripos_custom($text, "php");
  $position = mb_stripos_custom($text, "php");
  echo "Case-insensitive position: " . $position . "\n";
  echo "Case-insensitive position: " . $position . "\n";
</syntaxhighlight>
<br><br>
== 文字列の置換 ==
str_replace関数は文字列の置換を行う関数である。<br>
<br>
str_replace($search, $replace, $subject [, &$count])
# 第1引数 : 検索する文字列または配列
# 第2引数 : 置換する文字列または配列
# 第3引数 : 対象の文字列または配列
# 第4引数 : 置換が行われた回数 (オプション)
<br>
str_replace関数の特徴を以下に示す。<br>
* 大文字・小文字を区別する。<br>区別しない場合は、str_ireplace関数を使用する。
* 配列での複数置換が可能である。
* 文字列だけでなく配列も処理可能である。
* マルチバイト文字 (日本語等) も処理できる。
<br>
<u>注意</u><br>
* 配列を使用する場合、search配列とreplace配列の要素数が異なる場合は空文字が使用される。
* パフォーマンスを考慮する場合は、正規表現 (preg_replace関数) の使用も検討する。
* 大量のデータを処理する場合はメモリ使用量に注意する。
<br>
一般的な使用シーンを以下に示す。<br>
* テキストの整形
* HTMLの処理
* ファイル名の処理
* データのクリーニング
* フォーマット変換
<br>
==== 基本 ====
<syntaxhighlight lang="php">
$text = "Hello World";
$result = str_replace("World", "PHP", $text);
echo $result . "\n"; // "Hello PHP"
</syntaxhighlight>
<br>
==== 日本語 (マルチバイト文字) の置換 ====
<syntaxhighlight lang="php">
$text = "こんにちは世界";
$result = str_replace("世界", "みなさん", $text);
echo $result . "\n"; // "こんにちはみなさん"
</syntaxhighlight>
<br>
==== 複数の文字列を置換 ====
<syntaxhighlight lang="php">
$text = "りんごとバナナとみかん";
$search = ["りんご", "バナナ", "みかん"];
$replace = ["🍎", "🍌", "🍊"];
$result = str_replace($search, $replace, $text);
echo $result . "\n";  // "🍎と🍌と🍊"
</syntaxhighlight>
<br>
==== 置換回数のカウント ====
<syntaxhighlight lang="php">
$text = "The cat and the cat and the cat";
$count = 0;
$result = str_replace("cat", "dog", $text, $count);
echo $result . "\n";              // "The dog and the dog and the dog"
echo "置換回数: " . $count . "\n";  // "置換回数: 3"
</syntaxhighlight>
<br>
==== 大文字・小文字を区別しない置換 (str_ireplaceの使用) ====
<syntaxhighlight lang="php">
$text = "Hello HELLO hello";
$result = str_ireplace("hello", "hi", $text);
echo $result . "\n"; // "hi hi hi"
</syntaxhighlight>
<br>
==== 配列内の要素の置換 ====
<syntaxhighlight lang="php">
$fruits = ["apple", "banana", "apple", "orange"];
$result = str_replace("apple", "grape", $fruits);
print_r($result); // ["grape", "banana", "grape", "orange"]
</syntaxhighlight>
<br>
==== その他の使用例 ====
<syntaxhighlight lang="php">
// 使用例 1 : HTMLタグのエスケープ
function escapeHTML($text)
{
    $search = ['<', '>', '"', "'", '&'];
    $replace = ['&lt;', '&gt;', '&quot;', '&#39;', '&amp;'];
    return str_replace($search, $replace, $text);
}
$html = "<p>This is a 'test' & example</p>";
echo escapeHTML($html) . "\n";
// 使用例 2 : ファイル拡張子の変更
function changeFileExtension($filename, $newExt)
{
    $info = pathinfo($filename);
    return str_replace($info['extension'], $newExt, $filename);
}
$filename = "document.txt";
echo changeFileExtension($filename, "pdf") . "\n";  // "document.pdf"
// 使用例 3 : 電話番号のフォーマット
function formatPhoneNumber($phone)
{
    $phone = str_replace(['-', ' ', '(', ')'], '', $phone);
    return substr($phone, 0, 3) . '-' .
          substr($phone, 3, 4) . '-' .
          substr($phone, 7);
}
echo formatPhoneNumber("09012345678") . "\n";  // "090-1234-5678"
// 使用例 4 : 文字列のクリーニング
function cleanString($string)
{
    $unwanted = ["\r", "\n", "\t", "  "];
    $wanted = ["", " ", " ", " "];
    return trim(str_replace($unwanted, $wanted, $string));
}
$dirty = "This is a\n\t messy  string";
echo cleanString($dirty) . "\n";
  </syntaxhighlight>
  </syntaxhighlight>
<br><br>
<br><br>

案内メニュー