DOMDocument()를 이용해 입력된 string에서 html의 유효성을 체크해 보자

libxml_use_internal_errors(user_errors)
- libxml의 표준 에러 정보를 비활성화 하고, 사용자 정의 오류를 할 수 있도록 해줌.
- user_errors : option 항목으로 사용자 오류 처리가 가능할지 여부를 지정. 기본값은 false

libxml_clear_errors()
- libxml 오류 버퍼를 지움.

libxml_get_errors()
- libxml 오류 버퍼가 있을때 오류 정보를 배열로 반환. 없을때는 빈 배열을 반환함.


위 정보와 DOMDocument()를 이용해 입력된 string에서 html의 유효성을 체크해 보자

$string = '<div>안녕하세요</div><span>네. 안녕하세요';
$doc = new DOMDocument();
libxml_use_internal_errors(true);
libxml_clear_errors();
$doc_re = $doc->loadHTML($string);

if(count(libxml_get_errors())>0)  echo '에러 발생';

에러 존재 여부에 따라서 이후 처리를 해주면 되겠다.

데이타를 엑셀 시트로 작성시 유용한 스타일

데이타를 엑셀(excel) 시트로 작성시 유용한 스타일

 

1. 숫자를 일반문자 형태로 처리

style='mso-number-format:"\@";'

 

2. 숫자로 3자리마다(천단위) 콤마 찍기

style='mso-number-format:"\#\,\#\#0_;'

 

3. 한 셀(sale cell)에 줄바꿈(newline) 처리가 필요할때

style='mso-data-placement:same-cell;'

 

 

 

php soap 통신 개발시 'SoapClient' not found 에러 대처

Soap 통신시 Class 'SoapClient' not found 에러

SOAP(Simple Object Access Protocol)는 인터넷상에서 xml기반으로 메세지를 상호 교환할때 사용한다.

soap 통신 프로그램 개발시 "SoapClient" not found에러가 발생하게 되면
이는 서버에 soap 라이브러리가 활성화 되어 있지 않은것이다.

우선,
현재 서버에서 soap 관련 라이브러리가 설치되어 있나 확인을 한다.
간단히, phpinfo()로 확인을 해보면,
Soap Client enabled
Soap Server enabled
되어 있어야 한다.





만약, 없다면..
서버에서 soap 설치여부를 다시 확인
[root@~]rpm -qa | grep php-soap 엔터
 결과값이 없으면 설치해야 한다.

설치
[root@~]yum install php-soap 엔터
 파일 다운로드 받으면서 중간에 "Is this ok [y/n]" 나오면 Y 엔터
 complet! 메세지 출력이 되면서 설치 완료된다.

다시 체크
[root@~]rpm -qa | grep php-soap
php-soap-5.3.3-46.el6_6.x86_64

설치된것을 확인할 수 있다.

이제 서버를 재시작해주면 soap를 사용할 수 있다.
[root@~]service httpd restart

pdo로 prodcedure 사용시 잘못된 결과값을 보여줄때.

pdo를 사용해보려고, 서버 셋팅 및 기본 코딩을 잡아 보고 있던중에
스토어드프로시져(내부에 동적쿼리로 작성)를 사용중에 결과값이 잘못가져오는 경우가 발생.
결과값이 컬럼과 value가 제대로 매칭되지 않는 문제가 발생했다.
웹 서버 버젼은 
- php  version 5.1.6
- mysql client API version 5.0.37 
이다.
이 환경에서 동적쿼리가 포함된 프로시져 사용시 결과값이 잘못 가져오는 경우가 생겨서 한참 고생했다. 구글링을 한참 하다 보니, PDO::ATTR_EMULATE_PREPARES 이부분을 프로그램에 적용후 해당 문제가 해결되었다.
정확한 이유는 모르겠으나, 대략적으로 쿼리 파싱을 PDO쪽에서 할건지 mysql서버쪽에서 할건지를 결정하는거 같다. 이부분을 동적쿼리인경우 PDO쪽에서 하도록 해줘서 해결된 문제다.(prepared를 client 또는 server쪽에서 하도록 정하는 부분인거 같은데, 서버쪽에서 하는것이 10%정도 더 효과가 있다고 한다.)
PDO에 대한 버젼에 따라서 이런 문제가 있지 않나 싶다.

프로그램 부분은

$dbh = new PDO('mysql:host=db서버;dbname=데이타베이스', '계정','비밀번호',array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES euckr"));
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);

이렇게 해주면된다.
이부분이 없이 PDO사용이 가능하다면 빼주는것이 좋다고 한다. 

이걸 해결해 볼려고, 구글링을 하다가 찾은 여러 포스트중 아래 두개 포스트에서 많은 이해를 할수 있었다.(영문이라, 나름 이해하는라 힘들었음..ㅠㅠ)
http://wezfurlong.org/blog/2006/apr/using-pdo-mysql/
http://isp-control.net/ispcp/ticket/2402
인용 : 

The "ATTR_EMULATE_PREPARES" attribute allows to emulate the prepared queries for RDBMS that don't support this feature. By default, emulation is actives (ATTR_EMULATE_PREPARES == TRUE). That means that the queries will not be prepared by the Mysql Server (Server-Side) but by PDO (Client-Side). The final result is identical, all the parameters are automatically quoted and so, we have not problem with SQL injection in both cases.

Now, what is the subject here ? What the difference with the native and emulated support for prepared queries ?

Some RDBMS don't support prepared queries. That means that the queries will be prepared by PDO (

Client-Side

) and not by the Server (

Server-side

). The final result is the same but for performances reasons, it's better to use native support when it's possible. Indeed, for example, with Mysql, we can take 10 % free.

Th remaining problem with Mysql is that for versions prior 5.1.17 (in reality 5.1.21 for us), the prepared queries can't be cached. So, we have 10 % free but we lost 20 - 35 %...

That for why, the native support should be used when possible (only with Mysql server >= 5.1.21 (for us).

To resume, the parameter ATTR_EMULATE_PREPARES should be set to TRUE for Mysql server version prior 5.1.21 and to FALSE for other versions. By default the ATTR_EMULATE_PREPARESis set to TRUE.

TRUE: Support for prepared queries is emulated. FALSE: Support for prepared queries is not emulated. The native support is used.
 

나중에 서버 php버젼을 업그래이드 하게되면, 다시한번 살펴봐야겠다.


 

pdo_mysql 확장 모듈 올리기

pdo_mysql을 이용하기 위해서 pdo_mysql 확장모듈을 서버에 설치해보자.
(개개의 서버마다 서버 설정 및 모듈 설치 경로가 다를수 있으니, 이부분은 환경에 맞게 확인을 해서 설치를 한다.)

linux 웹서버에 로그인한후,
//pdo확장모듈을 다운받을경로로 이동
#cd /usr/local/src
//pdo확장모듈 최신 다운.
#wget http://pecl.php.net/get/PDO_MYSQL-1.0.2.tgz 
//압축해제.
#tar -xzvf PDO_MYSQL-1.0.2.tgz
//압축해제한 pdo모듈 dir로 이동.
#cd PDO_MYSQL-1.0.2
//phpize(php의 확장모듈을 올리는 유틸)위치 확인
#whereis phpize  (whereis로 못찾을경우 find -name phpzie 으로 찾아본다)
#/usr/local/bin/phpize
//php-config 위치 확인
#whereis php-config
//설치하기
#./configure --with-php-config=/usr/local/bin/php-config --with-pdo-mysql=/usr/local/mysql
#make
#makeinstall
//extension dir로 이동후 pdo_mysql.so 존재여부확인.
#cd /usr/local/lib/php/extenstions/no-debug-zts-2006613
#ls pdo_mysql.so

여기까지 pdo_mysql 확장모듈 설치가 끝났다.
이제 프로그램에서 실제로 사용하기 위해서 php.ini 추가를 해준다.

//php.ini에
#vi /usr/local/apache/conf/php.ini
extension=pdo_mysql.so 추가
//아파치 리스타트

이제 pdo를 맘껏 사용해보자...^^


*phpize ?
이건, php 확장 모듈을 php재컴파일을 하지 않고 설치해주는 유틸이란다.
사용방법은 위에 pdo_mysql 설치 예시에서 처럼,
phpize 실행 --> ./configure 환경설정 --> make 를 해주면된다고 한다.


중복되지 않는 랜덤 숫자 가져오기

배열처리중 중복되지 않는 순번으로 출력시키기.
총 10개의 데이타중에 중복되지 않게 5개를 가져오기.

$total_cnt = 10;
$k_nums = range(0,$total_cnt-1);
shuffle($k_nums);                       
for($i=0;$i<$row_cnt;$i++){
    if($i>5)    break;
    $k=$k_nums[$i];        //랜덤 번호 가져오기.
    echo $k ."\t";
}

이런게 필요할때가 있다.
db에서 랜덤으로 뽑으면 좋겠지만, 그렇지 못한경우 위와 같이 하면 중복되지않게 출력을 할수 있다.