"PHP str replace()"의 두 판 사이의 차이

6번째 줄: 6번째 줄:


<syntaxhighlight lang="php" run>
<syntaxhighlight lang="php" run>
echo str_replace("f", "jump", "effffff"); # ejumpjumpjumpjumpjumpjump
echo str_replace('f', 'jump', 'effffff');
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang="php" run>
<syntaxhighlight lang="php" run>
$bodytag = str_replace("%body%", "black", "<body text='%body%'>");
$bodytag = str_replace('%body%', 'black', '<body text='%body%'>');
echo $bodytag;
echo $bodytag;
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang="php" run>
<syntaxhighlight lang="php" run>
$vowels = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"];
# 모음 제거
$onlyconsonants = str_replace($vowels, "", "Hello World of PHP");
$vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'];
$onlyconsonants = str_replace($vowels, '', 'Hello World of PHP');
echo $onlyconsonants;
echo $onlyconsonants;
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang="php" run>
<syntaxhighlight lang="php" run>
$phrase  = "You should eat fruits, vegetables, and fiber every day.";
$phrase  = 'You should eat fruits, vegetables, and fiber every day.';
$healthy = ["fruits", "vegetables", "fiber"];
$healthy = ['fruits', 'vegetables', 'fiber'];
$yummy  = ["pizza", "beer", "ice cream"];
$yummy  = ['pizza', 'beer', 'ice cream'];
$newphrase = str_replace($healthy, $yummy, $phrase);
$newphrase = str_replace($healthy, $yummy, $phrase);
echo $newphrase;
echo $newphrase;
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang="php" run>
<syntaxhighlight lang="php" run>
$str = str_replace("ll", "", "good golly miss molly!", $count);
$str = str_replace('ll', '', 'good golly miss molly!', $count);
var_dump( $str );
var_dump( $str );
var_dump( $count );
var_dump( $count );

2021년 8월 25일 (수) 14:28 판

1 개요

PHP str_replace()
  • 발견한 모든 검색 문자열을 치환 문자열로 교체하는 PHP 함수
  • 복잡한 치환 규칙(예: 정규표현식)이 필요하지 않을 때 사용하자.
echo str_replace('f', 'jump', 'effffff');
$bodytag = str_replace('%body%', 'black', '<body text='%body%'>');
echo $bodytag;
# 모음 제거
$vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'];
$onlyconsonants = str_replace($vowels, '', 'Hello World of PHP');
echo $onlyconsonants;
$phrase  = 'You should eat fruits, vegetables, and fiber every day.';
$healthy = ['fruits', 'vegetables', 'fiber'];
$yummy   = ['pizza', 'beer', 'ice cream'];
$newphrase = str_replace($healthy, $yummy, $phrase);
echo $newphrase;
$str = str_replace('ll', '', 'good golly miss molly!', $count);
var_dump( $str );
var_dump( $count );

2 같이 보기

3 참고

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}