Computer >> 컴퓨터 >  >> 프로그램 작성 >> PHP

문자열에서 영숫자가 아닌 문자를 제거하는 PHP 프로그램

<시간/>

문자열에서 영숫자가 아닌 문자를 제거하려면 코드는 다음과 같습니다. -

예시

<?php
$my_str="Thisis!@sample*on&ly)#$";
$my_str = preg_replace( '/[^a-z0-9]/i', '', $my_str);
echo "The non-alphanumeric characters removed gives the string as ";
echo($my_str);
?>
로 표시됩니다.

출력

The non-alphanumeric characters removed gives the string as Thisissampleonly

'preg_replace' 함수는 문자열에서 영숫자 문자를 제거하는 데 사용됩니다. 정규식은 영숫자를 필터링하는 데 사용됩니다. 문자열은 미리 정의되어 있고 이에 'preg_replace' 함수가 호출되어 콘솔에 재구성된 문자열이 표시됩니다.

예시

<?php
$my_str="This!#is^&*a)(sample*+_only";
$my_str = preg_replace( '/[W]/', '', $my_str);
echo "The non-alphanumeric characters removed gives the string as ";
echo($my_str);
?>
로 표시됩니다.

출력

The non-alphanumeric characters removed gives the string as Thisisasample_only

여기서 유일한 차이점은 다른 정규식이 사용된다는 것입니다. 이전 정규식과 의미는 같지만 다른 방식으로 작성됩니다.