PHP 트림 () 함수
예
합니다 (의 "그"와 "세계"에서 "안녕하세요", "라!") 문자열의 왼쪽을 제거합니다
<?php
$str = "Hello World!";
echo $str . "<br>";
echo trim($str,"Hed!");
?>
$str = "Hello World!";
echo $str . "<br>";
echo trim($str,"Hed!");
?>
»인스턴스를 실행
정의 및 사용
트림 () 함수는 공백 문자 또는 다른 미리 정의 된 문자열의 양면을 제거합니다.
관련 기능 :
문법
trim( string,charlist )
参数 | 描述 |
---|---|
string | 必需。规定要检查的字符串。 |
charlist | 可选。规定从字符串中删除哪些字符。如果省略该参数,则移除下列所有字符:
|
기술 세부 사항
반환 값 : | 수정 된 문자열을 반환합니다. |
---|---|
PHP 버전 : | 4+ |
업데이트 로그 : | PHP 4.1에서 charlist 매개 변수를 추가합니다. |
더 많은 예제
예 1
양쪽에 공간의 문자열을 제거합니다
<?php
$str = " Hello World! ";
echo "Without trim: " . $str;
echo "<br>";
echo "With trim: " . trim($str);
?>
$str = " Hello World! ";
echo "Without trim: " . $str;
echo "<br>";
echo "With trim: " . trim($str);
?>
(소스보기) 다음과 같이 코드의 HTML 출력은 위이다 :
<!DOCTYPE html>
<html>
<body>
Without trim: Hello World! <br>With trim: Hello World!
</body>
</html>
<html>
<body>
Without trim: Hello World! <br>With trim: Hello World!
</body>
</html>
다음과 같이 위의 코드의 브라우저 출력은 다음과 같습니다
Without trim: Hello World!
With trim: Hello World!
With trim: Hello World!
»인스턴스를 실행
예 2
줄 바꿈 (\ n을)의 양면에 문자열을 제거합니다
<?php
$str = "nnnHello World!nnn";
echo "Without trim: " . $str;
echo "<br>";
echo "With trim: " . trim($str);
?>
$str = "nnnHello World!nnn";
echo "Without trim: " . $str;
echo "<br>";
echo "With trim: " . trim($str);
?>
(소스보기) 다음과 같이 코드의 HTML 출력은 위이다 :
<!DOCTYPE html>
<html>
<body>
Without trim:
Hello World!
<br>With trim: Hello World!
</body>
</html>
<html>
<body>
Without trim:
Hello World!
<br>With trim: Hello World!
</body>
</html>
다음과 같이 위의 코드의 브라우저 출력은 다음과 같습니다
Without trim: Hello World!
With trim: Hello World!
With trim: Hello World!
»인스턴스를 실행
PHP 문자열 참조