s

PHP의 기본 str_replace 함수 보완 - 문자(열) 치환 완전 소거형식

<?php
header('Content-Type: text/html; charset=utf-8');

// str_lreplace 커스텀 함수의 (l)replace 소문자 '엘'은 Loop의 줄임말

function str_lreplace($find, $replace, $strings)
{
if ($strings && $find && $find!=$replace) {
if (stripos($replace, $find) !== false) {
$strings = str_ireplace($find, $replace, $strings);
}
else {
while(stripos($strings, $find) !== false) {
$strings = str_ireplace($find, $replace, $strings);
}
}
}
return $strings;
}


$test_str = '테스트 : testtesttestttesttestah!!testtesttesttesttesttesttesttttsseets 어지러워!';

$test_str_original = str_ireplace('testtest', 'test', $test_str);
$test_str_loop = str_lreplace('testtest', 'test', $test_str);

echo 'original text - '.$test_str;
echo '<br />';
echo 'str_ireplace - '.$test_str_original;
echo '<br />';
echo 'str_lreplace - '.$test_str_loop;
?>



[결과]
original text - 테스트 : testtesttestttesttestah!!testtesttesttesttesttesttesttttsseets 어지러워!
str_ireplace - 테스트 : testtestttestah!!testtesttesttesttttsseets 어지러워!
str_lreplace - 테스트 : testttestah!!testtttsseets 어지러워!
|

댓글 2개

오오 좋습니다.
좋아요..잘쓰겠습니다.감사합니다.
댓글을 작성하시려면 로그인이 필요합니다.

그누보드5 팁자료실

+
제목 글쓴이 날짜 조회
8년 전 조회 1.3만
8년 전 조회 4,238
8년 전 조회 6,687
8년 전 조회 4,617
8년 전 조회 5,813
8년 전 조회 6,270
8년 전 조회 5,996
8년 전 조회 6,933
8년 전 조회 6,051
8년 전 조회 4,484
8년 전 조회 9,949
8년 전 조회 4,716
8년 전 조회 4,322
8년 전 조회 5,350
8년 전 조회 4,425
8년 전 조회 6,506
8년 전 조회 5,903
8년 전 조회 4,820
8년 전 조회 4,333
8년 전 조회 4,765
8년 전 조회 6,778
8년 전 조회 4,715
8년 전 조회 6,695
8년 전 조회 5,399
8년 전 조회 4,867
8년 전 조회 7,572
8년 전 조회 6,121
8년 전 조회 7,933
8년 전 조회 6,501
8년 전 조회 4,291