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개

© SIRSOFT
현재 페이지 제일 처음으로