LoginSignup
0
0

More than 1 year has passed since last update.

str_replace str_ireplace preg_replace 文字列を置換 PHP

Last updated at Posted at 2022-04-25

用途

対象の値の中に指定した文字列が存在した場合に、その文字列全てを置換したい時に使う。

使用方法

str_replace

書き方
str_replace( $検索したい文字列, $置換したい文字列, $検索対象の文字列, int &$count )

第1引数
検索したい文字列。この文字列があったときに置換する。
第2引数
置換したい文字列。この文字列に置換する。
第3引数
配列などを置換した際に、置換した回数が表示される。省略可能。

$hoge = "red, green, blue";
$hogehoge = str_replace('red', 'yellow', $hoge);

echo $hogehoge;
// 結果
"yellow, green, blue"

※日本語の置換も可能

str_ireplace

文字の大小を区別せずに置換する。使い方は同じ。

preg_replace

正規表現で検索する。第1引数に検索したい正規表現を入れる。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0