PHP의 기본 str_replace 함수 보완 - 문자(열) 치환 완전 소거형식 > 그누보드5 팁자료실

그누보드5 팁자료실

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

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 어지러워!
추천
1

댓글 2개

전체 2,427 |RSS
그누보드5 팁자료실 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT