▶ trim() : 화이트스페이스를 제거


▶ strlen() : 문자열의 길이를 알려준다.


▶ printf() 이용해 0으로 채우기


1
2
3
4
5
6
      $zip = '6520';
      $month = 2;
      $day = 6;
      $year = 2007;
 
      printf("우편번호는 %05d이고 날짜는 %02d/%02d/%d", $zip, $month, $day, $year);
cs

결과 : 우편번호는 06520이고 날짜는 02/06/2007


▶ strcasecmp() : 대소문자 관계없이 문자열 비교하기

1
2
3
4
5
6
7
    $string1 = 'aaaaa';
 
    if(strcasecmp($string1, 'AAAAA') == 0){
        echo "같다";
    }else{
        echo "다르다";
    }
cs


▶ strtolower() : 모두소문자로


▶ strtoupper() : 모두대문자로


▶ ucwords(strtolower('JOHN FRANKENHEIMER')); : 앞글자만 대문자로 (John Frankenheimer)


substr() : 문자열 줄이기(원하는 일부만 추출할 수 있다. 문자열 첫 글자부터 시작해30바이트를 추출하라)

1
2
3
4
5
    $_POST['comments'] = "The Fresh Fish with Rice Noodle was delicious, but I didn't";
 
    echo substr($_POST['comments'], 0, 30);
    
    echo '...';
cs

결과 : The Fresh Fish with Rice Noodl...

1
2
3
$_POST['card'] = '4000-1234-6578-9101';
 
    echo substr($_POST['card'], -5);
cs
결과: -9101


▶ str_replace() : 문자열 일부를 치환해준다.

1
2
3
4
5
6
    $html = '<span class="{class}">유부</span> 
            <span class="{class}">생선튀김</span>';
 
    $my_class = 'lunch';
            
    echo str_replace('{class}', $my_class, $html);    
cs

class의 name이 lunch로 치환되었다.




+ Recent posts