Open1

文字列の置換 php

hisannhisann

str_replace

文字列の置換

str_replace( $検索文字列 , $置換後文字列 , $検索対象文字列 [, int &$count ] )

引数の解説
引数1:検索文字列
引数2:置換文字列
引数3:検索対象の指定
引数4(指定した場合):置換した回数

砕くと、
3の中の1を2に置換する。置換した回数を知りたい場合4を指定。

$text = 'apple,orange,apple,orange';

//指定した文字列が一致したら置き換える
$replace = str_replace('apple', 'melon', $text);

echo $replace;

実行結果

melon,orange,melon,orange

str_ireplace

str_replaceとの相違点
str_ireplaceは大文字と小文字を区別しない。