Html & Script

문자열 스크립트 종합선물세트

컨텐츠 정보

본문

http://mygony.com
 http://mygony.com/tt/index.php?pl=44&nc=1



자바스크립트용으로 PHP의 함수들을 흉내내본 것입니다.
더 필요한 게 있으면 그때그때 추가할 예정입니다.
기능 건의 받습니다. -_- v

prototype 으로 정의되어있어서...

("<b>test</b>").stripTags();

혹은

var str = "  !!!! ";
str = str.trim();

과 같이 사용가능합니다.

---------------------
// HTML 특수문자를 변환
String.prototype.htmlChars = function () {
      var str = ((this.replace('"', '&amp;')).replace('"', '&quot;')).replace('\'', '&#39;');
      return (str.replace('<', '<')).replace('>', '>');
}

// 좌우 공백없애는 함수
String.prototype.trim = function () { return this.replace(/(^s*)|(s*$)/g, ""); }

// 왼쪽 공백없애는 함수
String.prototype.ltrim = function () { return this.replace(/^s*/g, ""); }

// 오른쪽 공백없애는 함수
String.prototype.rtrim = function () { return this.replace(/s*$/g, ""); }

// 태그만 제거
String.prototype.stripTags = function () {
      var str = this;
      var pos1 = str.indexOf('<');

    if (pos1 == -1) return str;
    else {
        var pos2 = str.indexOf('>', pos1);
        if (pos2 == -1) return str;
        return (str.substr(0, pos1) + str.substr(pos2+1)).stripTags();
    }
}

// 대소문자 구별하지 않고 단어 위치 찾기
String.prototype.ipos = function (needle, offset) {
      var offset = (typeof offset == "number")?offset:0;
      var pos1 = str.toLowerCase().indexOf(needle.toLowerCase(), offset);
      var pos2 = str.toUpperCase().indexOf(needle.toUpperCase(), offset);

      if (pos1 == -1 && pos2 == -1) return false;
      if (pos1 == -1) pos1 = str.length + 1;
      if (pos2 == -1) pos2 = str.length + 1;

      return Math.min(pos1, pos2);
}

// 대소문자 구별하지 않고 뒤에서부터 단어위치 찾기
String.prototype.ripos = function (needle, offset) {
      var offset = (typeof offset == "number")?offset:0;
      var pos1 = str.toLowerCase().lastIndexOf(needle.toLowerCase(), offset);
      var pos2 = str.toUpperCase().lastIndexOf(needle.toUpperCase(), offset);

      if (pos1 == -1 && pos2 == -1) return false;
      return Math.max(pos1, pos2);
}

// 문자열을 배열로
String.prototype.toArray = function () {
      var len = this.length;
      var arr = new Array;
      for (var i=0; i<len; i++) arr[i] = this.charAt(i);
      return arr;
}
---------------------

관련자료

댓글 0
등록된 댓글이 없습니다.
Today's proverb
해가 들면 어떻고, 바람이 불면 어떻고, 눈이 오면 어떠랴. 해가 들어주어도 고맙고, 바람이 불어주어도 고맙고, 눈이 와주어도 고마울 뿐. 그렇다, 고맙지 않은 것이 없다. 밤은 밤이어서 고맙고, 새벽은 새벽이어서 고맙고, 낮은 낮이어서 고맙다. 아, 고마운 삼라만상이여! (정채봉)