Programming/PHP

[PHP] 문자열내 특정 문자 바꾸기 (str_replace)

DOTI 2015. 9. 8. 17:36
[PHP] 문자열내 특정 문자 바꾸기 (str_replace)
반응형

str_replace 함수를 이용하여 문자열 내에 특정 문자 치환


[기본 사용범]

str_replace(치환할문자,치환될문자,문자열);



[기본예제]

1
2
3
4
5
<?
$word = "abcde";
$word = str_replace("a","1",$word);
echo $word;
?>
cs


[기본예제결과]

1bcde



[예제1] 영문숫자 조합의 문자열에서 숫자를 제외시키는 예제

1
2
3
4
5
6
<?
$word = "a0b1c2d3e4f5g6h7i8j9";
$num = array("0""1""2""3""4""5""6""7""8""9");
$val = str_replace($num,"",$word);
echo $val;
?>
cs


[예제1 결과]

abcdefghij



[예제2] 숫자로된 문자열을 영문으로 치환

1
2
3
4
5
6
7
<?
$word = "12345";
$num = array("0""1""2""3""4""5""6""7""8""9");
$char = array("a""b""c""d""e""f""g""h""i""j");
$val = str_replace($num,$char,$word);
echo $val;
?>
cs


[예제2 결과]

bcdef



반응형