php 문자열에서 숫자만 추출하기 > PHP

본문 바로가기

사이트 내 전체검색

뒤로가기 PHP

php 문자열에서 숫자만 추출하기

페이지 정보

작성자 최고관리자 작성일 21-02-09 11:48 조회 6,475 댓글 0

본문

$text = "0#12.3f4hhds56.7dd890";

 $StelInt = ereg_replace("[^0-9]", "", $text);
  또는
$StelInt = preg_replace ("/[^0-9]*/s", "", $text);

 --> 하단은 일반적인 전화번호 보는 형식으로 만들기
if(substr($StelInt,0,1) == 0)
 {
        if(substr($StelInt,0,2) == 02)
        {
                $Stel =substr($StelInt,0,2) . "-" . substr($StelInt,2,-4)  . "-" . substr($StelInt,-4);
        }
        else
        {
                $Stel =substr($StelInt,0,3) . "-" . substr($StelInt,3,-4)  . "-" . substr($StelInt,-4);
        }       
 }
 else
 {
        $Stel =substr($StelInt,0,-4)  . "-" . substr($StelInt,-4);
 }

 echo "org $text <br>Stel $Stel";

 --> result <---
org 0#12.3f4hhds56.7dd890
 Stel 012-3456-7890

 =====================================================
밑에는 다른 방법

$hp1 = substr($StelInt,0,3);
 $hp2 = substr(substr($StelInt,0,-4),3);
 $hp3 = substr($StelInt,-4);

댓글목록 0

등록된 댓글이 없습니다.

Copyright © beautipia.co.kr. All rights reserved.

contact : webmaster@beautipia.co.kr

PC 버전으로 보기