$input = 'こんにちは、PHP!';
$pattern = '/こんにちは/';  // 「こんにちは」を検索する正規表現
$replacement = 'やあ';      // 「こんにちは」を「やあ」に置き換える

$output = preg_replace($pattern, $replacement, $input);

echo "Original input: $input";  // 出力: "Original input: こんにちは、PHP!"
echo "Output: $output";         // 出力: "Output: やあ、PHP!"
preg_replace(
    string|array $pattern,
    string|array $replacement,
    string|array $subject,
    int $limit = -1,
    int &$count = null
): string|array|null

// preg_replace(正規表現パターン、置き換える文字列、対象の文字列[、最大置換回数[、カウント変数]]);
$original_string = 'Hello, world! Hello, PHP! Hello, universe!';
$pattern = '/Hello/';
$replacement = 'Hi';

$new_string = preg_replace($pattern, $replacement, $original_string);

echo $new_string;  // 出力: 'Hi, world! Hi, PHP! Hi, universe!'
$original_string = 'Apples are red. Bananas are yellow. Apples and bananas are delicious.';
$search = ['/Apples/', '/Bananas/'];  // 正規表現を配列で指定
$replace = ['Oranges', 'Grapes'];     // 置換する文字列を配列で指定

$new_string = preg_replace($search, $replace, $original_string);

echo $new_string;
// 出力: 'Oranges are red. Grapes are yellow. Oranges and bananas are delicious.'
$original_string = 'PHP is a popular scripting language. PHP stands for PHP: Hypertext Preprocessor.';
$pattern = '/PHP/';
$replacement = 'PHP';

$count = 0;  // 置換回数を格納する変数を初期化

$new_string = preg_replace($pattern, $replacement, $original_string, -1, $count);

echo $new_string; // 出力: 'PHP is a popular scripting language.'
echo "The word 'PHP' appeared {$count} times.";
// 出力: "The word 'PHP' appeared 3 times."