2004. 7. 24. 21:09
기술문서창고/php
테터툴즈 블로그를 분석하면서 알게된 유용한 함수들
배열관련함수 - current(), prev(), next(), reset()
문자열처리함수 - addslashes(), stripslashes(), htmlspecialchars()
'&' (ampersand) becomes '&'
'"' (double quote) becomes '"' when ENT_NOQUOTES is not set.
''' (single quote) becomes ''' only when ENT_QUOTES is set.
'<' (less than) becomes '<'
'>' (greater than) becomes '>'
list - list()
url분석 - parse_url()
배열관련함수 - current(), prev(), next(), reset()
[CODE]<?php
$array = array('step one', 'step two', 'step three', 'step four');
// by default, the pointer is on the first element
echo current($array)."<br/>
"; // step "one"
// skip two steps
next($array);
next($array);
echo current($array)."<br/>
"; // "step three"
// reset pointer, start again on step one
reset($array);
echo current($array)."<br/>
"; // "step one"
?>[/CODE]
문자열처리함수 - addslashes(), stripslashes(), htmlspecialchars()
' " 같은 문자 앞에 를 추가해준다.
[CODE]<?php
$str = "Is your name O'reilly?";
// Outputs: Is your name O'reilly?
echo addslashes($str);
?>[/CODE]
' " 앞의 를 제거한다.[CODE]<?php
$str = "Is your name O'reilly?";
// Outputs: Is your name O'reilly?
echo stripslashes($str);
?>[/CODE]
다음 글자를 바꾼다(escape).'&' (ampersand) becomes '&'
'"' (double quote) becomes '"' when ENT_NOQUOTES is not set.
''' (single quote) becomes ''' only when ENT_QUOTES is set.
'<' (less than) becomes '<'
'>' (greater than) becomes '>'
[CODE]<?php
$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new; // <a href='test'>Test</a>
?>[/CODE]
list - list()
[CODE]<?php
$info = array('coffee', 'brown', 'caffeine');
// Listing all the variables
list($drink, $color, $power) = $info;
print "$drink is $color and $power makes it special.
";
// Listing some of them
list($drink, , $power) = $info;
print "$drink has $power.
";
// Or let's skip to only the third one
list( , , $power) = $info;
print "I need $power!
";
?>[/CODE]
[CODE]<table>
<tr>
<th>Employee name</th>
<th>Salary</th>
</tr>
<?php
$result = mysql_query("SELECT id, name, salary FROM employees",$conn);
while (list($id, $name, $salary) = mysql_fetch_row($result)) {
print(" <tr>
" .
" <td><a href="info.php?id=$id">$name</a></td>
" .
" <td>$salary</td>
" .
" </tr>
");
}
?>
</table>[/CODE]
url분석 - parse_url()
다음과 같은 키값으로 쓰기 좋게 url을 잘라준다.
scheme - e.g. http
host
port
user
pass
path
query - after the question mark ?
fragment - after the hashmark #
[CODE]$ php -r 'print_r( parse_url("http://username:password@hostname/path?arg=value#anchor"));'
Array
(
[scheme] => http
[host] => hostname
[user] => username
[pass] => password
[path] => /path
[query] => arg=value
[fragment] => anchor
)
$ php -r 'print_r( parse_url("http://invalid_host..name/"));'
Array
(
[scheme] => http
[host] => invalid_host..name
[path] => /
)[/CODE]
posted by
progh2