'Language/PHP'에 해당되는 글 85건

  1. 2010.05.27 addslashes, stripslashes, htmlspecialchars
  2. 2010.05.27 preg_replace
  3. 2010.05.26 IF의 다른 표현 (Alternative syntax for IF statements): IF(): ... ENDIF;
  4. 2010.05.25 외부 링크 방지
  5. 2010.05.24 echo 를 이용한 문자 와 변수 출력 방법
  6. 2010.05.24 문자 출력 <<<EOD
  7. 2010.05.24 mysql_connect 접속 및 에러코드 확인
  8. 2010.05.18 array_count_values
  9. 2010.05.18 addslashes / stripslashes
  10. 2010.05.12 array_unshift, array_push

addslashes, stripslashes, htmlspecialchars

Language/PHP 2010. 5. 27. 09:54

No. 28

함수명

addslashes

의미

작은 따옴표(') 와 큰 따옴표(") 역 슬래쉬(\)와 같은 특정문자 앞에 역슬래쉬(\) 문자를 붙인다.

형식

string addslashes ( string $str )

매개변수

$str  : 변환할 문자열 문자

예)

<?

$comment = "안녕하세요 '공군식' 입니다.!";

echo "addslashes() 함수 호출전 : ".$comment."<p>";

 

$comment = addslashes($comment);

echo "addslashes() 함수 호출후 : ".$comment;

?>

결과

addslashes() 함수 호출전 : 안녕하세요 '공군식' 입니다.!

addslashes() 함수 호출후 : 안녕하세요 \'공군식\' 입니다.!

 

No. 29

함수명

stripslashes

의미

addslashes() 함수를 통해 역슬래쉬가 된 문자를 원상태로 돌린다.

형식

string stripslashes ( string $str )

매개변수

$str  : 변환할 문자열 문자

예)

<?

$comment = "안녕하세요 '공군식' 입니다.!";

echo "addslashes() 함수 호출전 : ".$comment."<p>";

$comment = addslashes($comment);

echo "addslashes() 함수 호출후 : ".$comment."<p>";

$comment = stripslashes($comment);

echo "stripslashes() 함수 호출후 : ".$comment;

?>

결과

addslashes() 함수 호출전 : 안녕하세요 '공군식' 입니다.!

addslashes() 함수 호출후 : 안녕하세요 \'공군식\' 입니다.!

stripslashes() 함수 호출후 : 안녕하세요 '공군식' 입니다.!

 

No. 30

함수명

htmlspecialchars

의미

HTML코드를 문자열 소스 그대로 출력한다.

형식

string htmlspecialchars ( string $string [, int $quote_style = ENT_COMPAT [, string $charset [, bool $double_encode = true ]]] )

매개변수

$string : 문자값 // $quote_style  $charset  $double_encode  각종 옵션값

예)

<?

$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);

echo $new;

?>

결과

<a href='test'>Test</a>

'Language > PHP' 카테고리의 다른 글

print_r(), var_dump(), var_export  (0) 2010.05.28
mysql_free_result  (0) 2010.05.28
preg_replace  (0) 2010.05.27
IF의 다른 표현 (Alternative syntax for IF statements): IF(): ... ENDIF;  (0) 2010.05.26
외부 링크 방지  (0) 2010.05.25
:

preg_replace

Language/PHP 2010. 5. 27. 09:44

preg_replace와 비슷한 함수가 ereg_replace,eregi_replace,str_replace 정도가 있겠죠..
하지만 속도면에서 가장 빠르게 나타나는 preg_replace에 대해서 설명하겠습니다.

사용법은 간단합니다.
예1) $text="333444555";
444를 666으로 바꿀려면,
$text2=preg_replace("/444/","666",$text); 이렇게 하면됩니다.
배열로 선언한뒤 한꺼번에 바꾸는 방법도 있습니다. 보통 게시판이나 쇼핑몰 스킨작업할때 많이 씁니다.
예2)간단한 게시판을 예로들면
$text="
<table name=tt width=100% border=0>
<tr>
<td>{width_id}</td>
<td>{width_title}</td>
<td>{width_name}</td>
<td>{width_date}</td>
<td>{width_hit}</td>
</tr>
</table>
";
$text1=array(
"/{width_id}/",
"/{width_title}/",
"/{width_name}/",
"/{width_date}/",
"/{width_hit}/"
);
$text11=array($bg_width[0],$bg_width[1],$bg_width[2],$bg_width[3],$bg_width[4]);//값은 지정해주어야 합니다.
$ptext=preg_replace($text1,$text11,$text);
echo $ptext;
간단하죠!

자료 출처 :http://www.phpschool.com/bbs2/inc_view.html?id=7499&code=tnt2&start=60&mode=&s_que=&field=&operator=&period=&category_id=

:

IF의 다른 표현 (Alternative syntax for IF statements): IF(): ... ENDIF;

Language/PHP 2010. 5. 26. 16:14

IF의 다른 표현 (Alternative syntax for IF statements): IF(): ... ENDIF;

PHP 3는 중괄호({ })를 쓰는 대신 IF(expr)뒤에 콜론( : )을 찍고, 하나 이상의 문장을 나열한 후에 ENDIF;로 끝내는 방법을 제공한다. 이 방법은 특히 IF문 안에 HTML 블럭을 삽입하는데 유용하게 사용될 수 있다. 다음 예를 보자. :

<?php if ($a==5): ?>
A = 5
<?php endif; ?>

위의 예에서 "A = 5"라는 HTML 블록이 IF문 안에 사용되고 있다. 위의 HTML 블록은 $a가 5일 경우에만 표시된다.

다음과 같이 ELSE와 ELSEIF (expr)도 사용할 수 있다. :

if ($a==5):
    print "a equals to 5";
    print "...";
elseif ($a==6):
    print "a equals to 6";
    print "!!!";
else
    print "a is not 5 nor 6";
endif;
 
출처 - http://www.realfire.com/php3menualkr.htm

'Language > PHP' 카테고리의 다른 글

addslashes, stripslashes, htmlspecialchars  (0) 2010.05.27
preg_replace  (0) 2010.05.27
외부 링크 방지  (0) 2010.05.25
echo 를 이용한 문자 와 변수 출력 방법  (0) 2010.05.24
문자 출력 <<<EOD  (0) 2010.05.24
:

외부 링크 방지

Language/PHP 2010. 5. 25. 18:43

php면 apache 웹서버를 사용하시겠네요..
서버에서 설정해주는 부분이 있습니다.

 

**/apache/conf/httpd.conf 나 혹은 vhost.conf 에서 VirtualHost 설정부분에 추가해주시면 됩니다.

 

<VirtualHost 211.55.28.16>
    ServerNamewww.lastcom.pe.kr
    DocumentRoot /home/html
    SetEnvIFNoCase Referer lastcom.pe.kr link_ok
    <FilesMatch ".(gif|jpg)$">
    Order deny,allow
    deny from all
    allow from env=link_ok
    </FilesMatch>
</VirtualHost>

 

즉 위처럼 셋팅하신후 아파치를 새로 구동하시면
gif나 jpg파일은 lastcom.pe.kr 에 한해서만 링크를 걸수 있게 됩니다.

출처 - 지식인

:

echo 를 이용한 문자 와 변수 출력 방법

Language/PHP 2010. 5. 24. 09:29

<?php
$beer 
'Heineken'
;
echo 
"$beer's taste is great"
// works; "'" is an invalid character for variable names
echo "He drank some $beers";   
// won't work; 's' is a valid character for variable names but the variable is "$beer"
echo "He drank some ${beer}s"
// works
echo "He drank some {$beer}s"
// works
?>

<?php
// These examples are specific to using arrays inside of strings.
// When outside of a string, always quote array string keys and do not use
// {braces}.

// Show all errors
error_reporting(E_ALL
);

$fruits = array('strawberry' => 'red''banana' => 'yellow'
);

// Works, but note that this works differently outside a string
echo "A banana is $fruits[banana]."
;

// Works
echo "A banana is {$fruits['banana']}."
;

// Works, but PHP looks for a constant named banana first, as described below.
echo "A banana is {$fruits[banana]}."
;

// Won't work, use braces.  This results in a parse error.
echo "A banana is $fruits['banana']."
;

// Works
echo "A banana is " $fruits['banana'] . "."
;

// Works
echo "This square is $square->width meters broad."
;

// Won't work. For a solution, see the complex syntax.
echo "This square is $square->width00 centimeters broad."
;
?>

'Language > PHP' 카테고리의 다른 글

IF의 다른 표현 (Alternative syntax for IF statements): IF(): ... ENDIF;  (0) 2010.05.26
외부 링크 방지  (0) 2010.05.25
문자 출력 <<<EOD  (0) 2010.05.24
mysql_connect 접속 및 에러코드 확인  (0) 2010.05.24
array_count_values  (0) 2010.05.18
:

문자 출력 <<<EOD

Language/PHP 2010. 5. 24. 09:27

<?php
$str 
= <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;

/* More complex example, with variables. */
class 
foo
{
    var 
$foo
;
    var 
$bar
;

    function 
foo
()
    {
        
$this->foo 'Foo'
;
        
$this->bar = array('Bar1''Bar2''Bar3'
);
    }
}

$foo = new foo
();
$name 'MyName'
;

echo <<<EOT
My name is "$name". I am printing some $foo->foo
.
Now, I am printing some 
{$foo->bar[1]}
.
This should print a capital 'A': \x41
EOT;
?>

'Language > PHP' 카테고리의 다른 글

외부 링크 방지  (0) 2010.05.25
echo 를 이용한 문자 와 변수 출력 방법  (0) 2010.05.24
mysql_connect 접속 및 에러코드 확인  (0) 2010.05.24
array_count_values  (0) 2010.05.18
addslashes / stripslashes  (0) 2010.05.18
:

mysql_connect 접속 및 에러코드 확인

Language/PHP 2010. 5. 24. 09:18

<?php
// on se connecte à example.com et au port 3307
$link mysql_connect('example.com:3307''mysql_user''mysql_password'
);
if (!
$link
) {
    die(
'Connexion impossible : ' mysql_error
());
}
echo 
'Connecté correctement'
;
mysql_close($link
);

// on se connect à localhost au port 3307
$link mysql_connect('127.0.0.1:3307''mysql_user''mysql_password'
);
if (!
$link
) {
    die(
'Connexion impossible : ' mysql_error
());
}
echo 
'Connecté correctement'
;
mysql_close($link
);


$link mysql_connect('example.com:3307''mysql_user''mysql_password') or die ("접속 할 수 없습니다.");
?>

'Language > PHP' 카테고리의 다른 글

echo 를 이용한 문자 와 변수 출력 방법  (0) 2010.05.24
문자 출력 <<<EOD  (0) 2010.05.24
array_count_values  (0) 2010.05.18
addslashes / stripslashes  (0) 2010.05.18
array_unshift, array_push  (0) 2010.05.12
:

array_count_values

Language/PHP 2010. 5. 18. 16:35

<?php
$array
= array (1, "hello", 1, "world", "hello"
);
print_r(array_count_values ($array
));  //결과값 Array ( [1] => 2 [hello] => 2 [world] => 1 )
?>

      
$array = array(1,"hello",1,"world","hello");

$abc = (array_count_values($array));

echo $abc[hello], $abc[1], $abc[world]; // 결과는 221 이 됩니다.

$array라는 배열안에 1이란 값이 두개, hello란 값이 2개, world란 값이 1개 들어 있다는 뜻...^^


출처 - http://cafe.naver.com/friendvirus

'Language > PHP' 카테고리의 다른 글

문자 출력 <<<EOD  (0) 2010.05.24
mysql_connect 접속 및 에러코드 확인  (0) 2010.05.24
addslashes / stripslashes  (0) 2010.05.18
array_unshift, array_push  (0) 2010.05.12
print_r  (0) 2010.05.11
:

addslashes / stripslashes

Language/PHP 2010. 5. 18. 10:34

 

addslashes($str);

 

문자열에 백슬래시(\) 를 붙인다.

쌍따옴표나 작은따옴표를 사용할 때 replace 해야 할 필요성이 있는 문자열에

addslashes 함수를 사용하면 된다.

 

특히 데이터베이스에는 작은따옴표(')가 들어가는 문자열을 넘겨주게되면 안된다.

즉, insert from table (name) values('sim'jin) 하나의 문자열을 작은따옴표(')로 구분하는

데이터베이스에서는 문자열을 닫는걸로 인식하게된다. 이때 replace 가 필요하게 된다.

작은따옴표를 아무런 문제없이 넣으려면 'sim\'jin' 으로 대입되어야 한다.

 

 

 

stripslashes($str);

addslashes 한 문자열을 되돌려주는 함수이다.

즉 백슬래시를 제거한 문자열을 리턴한다.

이중 백슬래시는 하나의 백슬래시로 만들게된다.

[출처] addslashes, stripslashes|작성자 심진


추가 - http://kr2.php.net/manual/kr/function.stripslashes.php

'Language > PHP' 카테고리의 다른 글

mysql_connect 접속 및 에러코드 확인  (0) 2010.05.24
array_count_values  (0) 2010.05.18
array_unshift, array_push  (0) 2010.05.12
print_r  (0) 2010.05.11
Foreach 문을 이용한 배열출력  (0) 2010.05.11
:

array_unshift, array_push

Language/PHP 2010. 5. 12. 10:35

array_unshift

(PHP 4, PHP 5)
array_unshift --  하나 이상의 요소를 배열의 최초로 더한다

설명

int array_unshift ( array &array, mixed var [, mixed ...] )

array_unshift() (은)는,array 의 선두로 지정된 요소를 더합니다.리스트의 요소는 전체적으로 더해지기 위해, 더해진 요소의 차례는 변하지 않는 것에 주의해 주세요. 배열의 수치 첨자는 모두 새롭게 제로로부터 거절해 더 됩니다. 리터럴의 키에 대해서는 변경되지 않습니다.
처리 후의 array 의 요소의 수를 돌려줍니다.
례 1. array_unshift() 의 예
<?php
$queue
= array("orange", "banana"
);
array_unshift($queue, "apple", "raspberry"
);
?>
위의 예의 출력은 이하가 됩니다.
Array
(
    [0] => apple
    [1] => raspberry
    [2] => orange
    [3] => banana
)
 
 

array_push

(PHP 4, PHP 5)
array_push --  하나 이상의 요소를 배열의 마지막에 추가한다

설명

int array_push ( array &array, mixed var [, mixed ...] )

array_push() (은)는,array (을)를 스택으로서 처리해, 건네받은 변수를 array 의 마지막에 더합니다.배열 array 의 길이는 건네받은 변수의 수만큼 증가합니다. 각 var 마다 이하를 반복하는 것으로 같은 효과가 있습니다.
<?php
$array
[] = $var
;
?>
var 그리고 반복해집니다.
처리 후의 배열안의 요소의 수를 돌려줍니다.
례 1. array_push() 의 예
<?php
$stack
= array("orange", "banana"
);
array_push($stack, "apple", "raspberry"
);
print_r($stack
);
?>
위의 예의 출력은 이하가 됩니다.
Array
(
    [0] => orange
    [1] => banana
    [2] => apple
    [3] => raspberry
)
주의: 만약 배열에 하나의 요소를 더하기 위해서 array_push() (을)를 사용한다면, 함수를 부르는 오버헤드가 없기 때문에,$array[] = (을)를 사용하는 편이 좋습니다.
주의: 최초의 인수가 배열이 아닌 경우,array_push() (은)는 경고를 발생시킵니다.이것은 신규 배열을 생성하는 경우에 있어서의 $var[] 의 동작과 다릅니다. 

'Language > PHP' 카테고리의 다른 글

array_count_values  (0) 2010.05.18
addslashes / stripslashes  (0) 2010.05.18
print_r  (0) 2010.05.11
Foreach 문을 이용한 배열출력  (0) 2010.05.11
array 배열 일차원과 다차원  (0) 2010.05.11
: