사이트 내 전체검색
URL 처리 관련 정규식
로빈아빠
https://cmd.kr/javascript/850 URL이 복사되었습니다.

본문

// url에 http:// 를 붙인다
    if (!preg_match("/^(http|https|ftp|telnet|news|mms)\:\/\//i", $url))
        $url = "http://" . $url;



<?php

$urls = Array();
$urls[] = 'http://apmusers.com/';
$urls[] = 'https://apmusers.com/';
$urls[] = 'HTTP://APMUSERS.COM/';
$urls[] = 'ahttp://apmusers.com/';//http:// 앞에 다른 문자가 있다.
$urls[] = 'b http://apmusers.com/';//htpp:// 앞에 다른 문자와 공백이 있다.
$urls[] = 'http:/apmusers.com/'; //http:/ 다음에 / 하나 없다

foreach($urls as $url){

    echo $url . ' ==> ';

    //url 의 제일 앞에 대소문자 구분없이 http:// 나 https:// 가 존재하는지 확인
    if (preg_match("`^https?://`i", $url)) {

        echo "<font color='blue'>제일 앞에 http(s):// 가 있는 사용 가능한 URL 입니다.</font> <br />" . PHP_EOL;
    }
    else {

        echo "<font color='red'>제일 앞에 http(s):// 가 없는 사용 불가능한 URL 입니다.</font> <br />" . PHP_EOL;
    }
}

?>


<?
$domain="www.naum.kr";
preg_match('/[^\.]+\.([^\.]{4}|[^\.]{3}|(co|or|pe|ac)\.[^\.]{2}|[^\.]{2})$/i', $domain, $matches);
print_r($matches);
?>

댓글목록

등록된 댓글이 없습니다.

831 (1/17P)

Search

Copyright © Cmd 명령어 3.12.71.237