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

잔글 (봇: 자동으로 텍스트 교체 (-</source> +</syntaxhighlight>, -<source +<syntaxhighlight ))
3번째 줄: 3번째 줄:
;PHP preg_replace()
;PHP preg_replace()


<source lang='PHP'>
<syntaxhighlight lang='PHP'>
$string = '135.79.246.80';
$string = '135.79.246.80';
$pattern = '/(\.\d+)$/';
$pattern = '/(\.\d+)$/';
9번째 줄: 9번째 줄:
echo preg_replace($pattern, $replacement, $string);
echo preg_replace($pattern, $replacement, $string);
# 135.79.246.x
# 135.79.246.x
</source>
</syntaxhighlight>
<source lang='PHP'>
<syntaxhighlight lang='PHP'>
$string = 'April 15, 2003';
$string = 'April 15, 2003';
$pattern = '/(\w+) (\d+), (\d+)/i';
$pattern = '/(\w+) (\d+), (\d+)/i';
16번째 줄: 16번째 줄:
echo preg_replace($pattern, $replacement, $string);
echo preg_replace($pattern, $replacement, $string);
# Aprilx,2003
# Aprilx,2003
</source>
</syntaxhighlight>
<source lang='PHP'>
<syntaxhighlight lang='PHP'>
$string = 'The quick brown fox jumps over the lazy dog.';
$string = 'The quick brown fox jumps over the lazy dog.';
$patterns = ['/quick/','/brown/','/fox/'];
$patterns = ['/quick/','/brown/','/fox/'];
23번째 줄: 23번째 줄:
echo preg_replace($patterns, $replacements, $string);
echo preg_replace($patterns, $replacements, $string);
# The bear black slow jumps over the lazy dog.
# The bear black slow jumps over the lazy dog.
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==

2020년 11월 2일 (월) 02:55 판

1 개요

PHP preg_replace()
$string = '135.79.246.80';
$pattern = '/(\.\d+)$/';
$replacement = '.x';
echo preg_replace($pattern, $replacement, $string);
# 135.79.246.x
$string = 'April 15, 2003';
$pattern = '/(\w+) (\d+), (\d+)/i';
$replacement = '${1}x,$3';
echo preg_replace($pattern, $replacement, $string);
# Aprilx,2003
$string = 'The quick brown fox jumps over the lazy dog.';
$patterns = ['/quick/','/brown/','/fox/'];
$replacements = ['bear','black','slow'];
echo preg_replace($patterns, $replacements, $string);
# The bear black slow jumps over the lazy dog.

2 같이 보기

3 참고

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