문자열 고급 함수

문자열다루기
---------------------------------------
문자열 합치기 (. / join()/ implode)
---------------------------------------
   echo $a.$b.$c;
echo "$a$b$c";
string = join("-", $배열)
$address[0] = "서울....";
$address[2] = "염창동 ....";
   $add = join(" ", $address);
string = implode("/", $배열)
---------------------------------------
문자열 자르기 (substr)---------------------------------------
string = substr(문자열, 시작점, 갯수)  
       주의점 :인덱스는 0부터 시작
         : 한글은 2바이트 처리됨
     문자열 길이 구하는 함수 strlen()이용하여 처리
     $length = strlen($filename);
     $ext = substr($filename, $length-3, 3);
               or
     $ext = substr($filename, -3);
---------------------------------------
문자찾기 [strpos(), strrpos(), strstr(), strrchr()]
---------------------------------------
int = strpos($문자열묶음, 찾을문자);
     주의점 : 인덱스 0부터 시작
int = strrpos($문자열묶음, 찾을문자); //오른쪽에서 부터 검색
     주의점 : 인덱스 0부터 시작
str = strstr($문자열묶음, 찾을문자)
   $문자열묶음에서 찾을문자 이후의 모든 내용을 출력
str = strrchr($문자열묶음, 찾을문자) //오른쪽에서 부터 검색

===================================================
일치하는 패턴찾기 preg_match()
===================================================
int = preg_match(str pattern, str subject, array[matches]);
subject 에서 pattern을 찾아 matches에 기억
matches의 첫번째 데이타는 패턴과 일치하는 데이타 기억
두번째 요소에는 패턴내에 괄호로 묶인 부분에 해당하는 데이터가 대입된다
pattern은 /로 묶어 표현
$subject ="Go to Page #9";
$pattern = "/[0-9]/";
if(preg_match($pattern, $subject , $matches))
echo "일치하는 패턴이 있다.". $matches[0];
else
echo "일치하는 패턴이 없다";
?>

패턴의 옵션
[] : 범위
+ : 반복
* : 0번이상 반복 0,1,2,3,....
$subject ="Go to page #9";
$pattern = "/([a-zA-Z]+ )+/";   ///알파벳 다음 빈칸 하나를 찾자 계속 .....
if(preg_match($pattern, $subject , $matches)){
    echo "일치하는 패턴은 ";
  for($i = 0 ; $i < count($matches); $i++){
  echo $i." ".$matches[$i]."
";   ///첫배째 요소엔 무조건 일치하는 내용 누적되어싸임ㅁ
          ///두배째 요소엔 () 패턴중 마지막 내용 들어간다
  }
} else {
   echo "일치하는 패턴없음 ";
}
?>

--------------------------------------------------------
년도에서 연월일 분리법
--------------------------------------------------------

  $date = "2004-09-17";
  if(preg_match("/([0-9]+)-([0-9]+)-([0-9]+)/" , $date, $ymd)) {
echo $ymd[0].$ymd[1]."/".$ymd[2]."/".$ymd[3];
} else{
echo "날짜 형식이 아님니다";
}
?>

======================================================
특정문자로 문자열 나누기  split() // explode
======================================================

array = explode(str 분리문자, str문자열)
      //전체 문자열에서 특정 문자열을 기준으로 잘라낸다
array = split(str pattern, str문자열, int [limit])
     // 단순문자열, 패턴을 사용 문자열을 나눌수 있으며 반환되는 배열
    원소의 수를 제한할수 있다
$data ="2004/09/17";
$dataTime ="2004/09/17 12:34:23";
list($year, $month, $day) = explode("/", $data);
list($year, $month, $day) = split("[/ :]", $dataTime);
echo "$year 년 $month 월 $day일 ";
?>
list() :함수 인자를 배열처럼 다루어 준다
문자열 치환  str_replace  /// substr_replace()
str = str_replace("찾아서", "바꾸고", "문자열묶음")
str = substr_replace(str, replacement, int start, int length);

   $strRel = str_replace("혼자","둘이", $str);
   $strRel2 = substr_replace($str, "둘이", strpos($str, "목"), 2);
echo $strRel;
?>
=========================================
html태그 변환하기
=========================================
htmlspecialchars() : 특정문자를 html 특수 표현으로 변경하기  
                  ///즉 소스로 표현 하겠다
      /// 브라우저로 해석하지 않고 뿌려라
                   " => &quto
     > =>>
nl2br()           : n -->
로 변경



=========================================
수 다루기
=========================================
int = floor(실수); :버림
int = ceil(실수);   : 올림
double = round(실수); 반올림

Press ESC to close