php에서 HTML, 공백, 줄 바꿈을 제거하고 일반 텍스트를 추출하는 방법: 1. 문자열 양쪽의 공백을 지웁니다. 코드는 [$str = Trim($str)]입니다. 2. 공백을 일치시킵니다. HTML에서 코드는 [$ str = preg_replace("/ /","",$str)]입니다.

php에서 HTML, 공백, 줄 바꿈을 제거하고 일반 텍스트를 추출하는 방법:
방법 1:
1 2 3 4 5 6 7 8 9 10 11 | function DeleteHtml( $str )
{
$str = trim( $str );
$str = preg_replace( "/\t/" , "" , $str );
$str = preg_replace( "/\r\n/" , "" , $str );
$str = preg_replace( "/\r/" , "" , $str );
$str = preg_replace( "/\n/" , "" , $str );
$str = preg_replace( "/ /" , "" , $str );
$str = preg_replace( "/ /" , "" , $str );
return trim( $str );
}
|
로그인 후 복사
Call 방법
DeleteHtml($str) </code ><code>DeleteHtml($str);
$str
$str
는 지워야 하는 페이지 문자열입니다
방법 2:
1 2 3 4 5 6 7 8 9 10 11 12 | function DeleteHtml( $str )
{
$str = trim( $str );
$str = strip_tags ( $str , "" );
$str = preg_replace( "/\t/" , "" , $str );
$str = preg_replace( "/\r\n/" , "" , $str );
$str = preg_replace( "/\r/" , "" , $str );
$str = preg_replace( "/\n/" , "" , $str );
$str = preg_replace( "/ /" , "" , $str );
$str = preg_replace( "/ /" , "" , $str );
return trim( $str );
}
|
로그인 후 복사
방법 3:
문자열 내부의 빈 줄 제거:
1 | $str = preg_replace( "/(s*?r?ns*?)+/" , "n" , $str );
|
로그인 후 복사
모두 제거 내부 및 정면을 포함한 빈 줄:
1 | $str = preg_replace('/( $s *$)|(^s*^)/m', '', $str );
|
로그인 후 복사
관련 학습 권장 사항: php 프로그래밍
(동영상)
🎜위 내용은 PHP에서 HTML, 공백, 줄 바꿈을 제거하고 일반 텍스트를 추출하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!