2014년 12월 12일 금요일

PHP 문자열 함수 정리

/*
nl2br
string = nl2br(string)
http://php.net/manual/en/function.nl2br.php
*/
echo nl2br("ABCD \n EFG \n\r");
echo nl2br("HIJK \r LMN \r\n");

/*
echo
void = echo(string)
http://php.net/manual/en/function.echo.php
*/
echo "ABCDEFG \n";
echo "HIJK","LMN","\n";
echo "OPQR
   STU
   \n";
echo ("VWXYZ"." ** \n");
echo <<< ALPHABET
ABCDEFGHIJKLMN
OPQRSTUVWXYZ.
ALPHABET;
$flag = TRUE;
echo $flag ? "TRUE" : "FALSE";

/*
print
int = print(string)
http://php.net/manual/en/function.print.ph
*/
$w = "WORLD";
print("HELLO~ $w !");

/*
sprintf
string = sprintf(string, mixed, mixed)
http://php.net/manual/en/function.sprintf.php
*/
$num = 5;
$location = 'tree';
$format = 'There are %d monkeys in the %s';
echo sprintf($format, $num, $location);

/*
printf
int = printf(string, mixed)
http://php.net/manual/en/function.printf.php
*/
printf("%d", "123.456");
printf("%2.2f", "123.4");
printf("%.2f", "3.141592653589793238462643383279502884197169399");

/*
sscanf
mixed = sscanf(string, string mixed)
http://php.net/manual/en/function.sscanf.php
*/
list($year, $month, $day, $serial) = sscanf("SN/19991231-A1234", "SN/%4d %2d %2d-%s%d");
echo "Item $serial was manufactured on: $year-" . $month . "-$day \n";

/*
explode
array = explode(string delimiter, string)
http://php.net/manual/en/function.explode.php
*/
$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0];
echo $pieces[5];
foreach($pieces as $key=>$value)
{
 echo $value . " ";
}

/*
implode 
string = implode(string, array)
http://php.net/manual/en/function.implode.php
*/
$pieces = array("piece1", "piece2", "piece3");
$pizza = implode(",", $pieces);
echo $pizza;

/*
strcmp
int = strcmp(string, string)
http://php.net/manual/en/function.strcmp.php
*/
$var1 = "Hello";
$var2 = "hello";
if(strcmp($var1, $var2) !== 0) // 0 is equal
{
    echo "$var1 <> $var2";
}
else
{
    echo "$var1 = $var2";
}

/*
strcasecmp
int = strcasecmp(string, string)
http://php.net/manual/en/function.strcasecmp.php
*/
$var1 = "Hello";
$var2 = "hello";
if(strcmp($var1, $var2) !== 0) // 0 is equal
{
    echo "$var1 = $var2";
}
else
{
    echo "$var1 <> $var2";
}

/*
strncmp
int = strncmp(string, string, int)
http://php.net/manual/en/function.strncmp.php
*/
if(strncmp("abc","aBC",2)==0) //if(!strncmp("abc","aBC",1))
{
 echo "equal";
}
else
{
 echo "not equal";
}

/*
strncasecmp
int = strcasecmp(string, string, int)
http://php.net/manual/en/function.strncasecmp.php
*/
if(strcasecmp("abc","aBC",2)==0) //if(!strncmp("abc","aBC",1))
{
 echo "equal";
}
else
{
 echo "not equal";
}

/*
substr
string = substr(string, int, int)
http://php.net/manual/en/function.substr.php
*/
echo substr("abcdefg",1);   // bcdefg
echo substr("abcdefg",0,1); // a
echo substr("abcdefg",-1);  // g
echo substr("abcdefg",-5,2);// cd

/*
strstr, strchr
string = strstr(string, string)
string = strchr(string, string)
http://php.net/manual/en/function.strstr.php
http://php.net/manual/en/function.strchr.php
*/
echo strstr("abcdefg","e");  // efg
echo strchr("YEAR-1999","-");// 1999

/*
stristr
string = stristr(string, string)
http://php.net/manual/en/function.stristr.php
*/
echo stristr("ABCDEFG","e");

/*
strrchr
string = strrchr(string, mixed)
http://php.net/manual/en/function.strrchr.php
*/
echo strrchr("hello1/hello2/hello3","/");

/*
strpos
int = strpos(string, mixed, int)
http://php.net/manual/en/function.strpos.php
*/
echo strpos("abcd/abcd/abcd","/");   // 4
echo strpos("abcd/abcd/abcd","/",5); // 9

/*
strrpos
int = strrpos(string, string, int)
http://php.net/manual/en/function.strrpos.php
*/
echo strrpos("abcd/abcd/abcd","/");    // 9
echo strrpos("abcd/abcd/abcd","/",11); // FALSE

/*
stripos
int = stripos(string, string, int)
http://php.net/manual/en/function.stripos.php
*/
echo stripos("abcd/ABCD/abcd","C"); // 2

/*
strripos
int = strripos(string, string, int)
http://php.net/manual/en/function.strripos.php
*/
echo strripos("abcd/ABCD/abcd","C"); // 12

/*
strlen
int = strlen(string)
http://php.net/manual/en/function.strlen.php
*/
echo strlen("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); // 26

/*
strspn
int = strspn(string, string, int, int)
http://php.net/manual/en/function.strspn.php
*/
echo strspn("12345", "1234567890abcdefghijklmnopqrstuvwxyz");  // 5
echo strspn("vwxyz", "1234567890abcdefghijklmnopqrstuvwxyz", 1,3); // wxy 3

/*
strcspn
int = strcspn(string, string, int, int)
http://php.net/manual/en/function.strcspn.php
*/
echo strcspn("12345", "1234567890abcdefghijklmnopqrstuvwxyz");  // 0
echo strspn("vwxyz", "1234567890abcdefghijklmnopqrstuvwxyz", 1,3); // wxy 3

/*
strnatcmp
int = strnatcmp(string, string)
http://php.net/manual/en/function.strnatcmp.php
*/
$str1 = "img11.png";
$str2 = "img2.png";
echo strcmp($str1,$str2); // -1
echo strnatcmp($str1,$str2); // 1

$arr1 = $arr2 = array("img12.png", "img10.png", "img2.png", "img1.png");

// Standard string comparison
usort($arr1, "strcmp"); // 1 10 12 2
print_r($arr1);

// Natural order string comparison
usort($arr2, "strnatcmp"); // 1 2 10 12
print_r($arr2);

/*
strnatcasecmp
int = strnatcasecmp(string, string)
http://php.net/manual/en/function.strnatcasecmp.php
*/
$str1 = "A.png";
$str2 = "a.png";
echo strcmp($str1,$str2); // -1
echo strnatcmp($str1,$str2); // -1
echo strnatcasecmp($str1,$str2); // 0

$arr1 = $arr2 = array("ABC.png", "abc.png", "aBC.png", "Abc.png");

usort($arr1, "strcmp"); // ABC Abc aBC abc
print_r($arr1);

usort($arr2, "strnatcmp"); // ABC Abc aBC abc
print_r($arr2);

usort($arr2, "strnatcasecmp"); // abc aBC Abc ABC
print_r($arr2);

/*
strtolower
string = strtolower(string)
http://php.net/manual/en/function.strtolower.php
*/
echo strtolower("ABC"); // abc

/*
strtoupper
string = strtoupper(string)
http://php.net/manual/en/function.strtoupper.php
*/
echo strtoupper("abc"); // ABC

/*
ucfirst
string = ucfirst(string)
http://php.net/manual/en/function.ucfirst.php
*/
echo ucfirst("abc"); // Abc

/*
ucwords
string = ucwords(string)
http://php.net/manual/en/function.ucwords.php
*/
echo ucwords("the quick brown fox jumps over the lazy dog"); 
// The Quick Brown Fox Jumps Over The Lazy Dog

/*
strrev
string = strrev(string)
http://php.net/manual/en/function.strrev.php
*/
echo strrev("olleh"); // hello

/*
strtr
string = strtr(string, string, string)
string = strtr(string, array)
http://php.net/manual/en/function.strtr.php
*/
echo strtr("olleh AB", "AB", "XYZ"); // olleh XY
echo strtr("hello apple", "e", "u"); // hullo applu
$arr = array("hello"=>"hell", "apple"=>"open");
echo strtr("hello apple", $arr); // hell open

/*
str_replace
mixed = str_replace(mixed, mixed, mixed)
http://php.net/manual/en/function.str-replace.php
*/
echo str_replace("apple", "world", "hello apple"); // hello world

$food = array("microsoft","banana","cherry","donuts","egg");
$arr = str_replace("microsoft","apple",$food);
print_r($arr); // apple, banana, cherry, donuts, egg

$search = array("microsoft","egg","cherry","banana","donuts");
$replace = array("soft-icecream","milk","berry");
$arr = str_replace($search,$replace,$food);
print_r($arr); // soft-icecream, ,berry, ,milk

$alphabet = array("a","b","c","d","e","f","g");
$search = array("a","b","c","d");
$replace = array("A","B","C");
$arr = str_replace($search,$replace,$alphabet,$count);
print_r($arr); // A, B, C, ,e, f, g
echo $count; // 4

/*
str_ireplace
mixed = str_ireplace(mixed, mixed, mixed)
http://php.net/manual/en/function.str-ireplace.php
*/
echo str_ireplace("ABC","XYZ","123abc"); // 123XYZ

/*
trim
string = trim(string)
http://php.net/manual/en/function.trim.php
*/
$text   = "\x0B\n\t\r\0 1 2 3 4 5 ";
var_dump($text); // 16
var_dump(trim($text)); // 9

/*
ltrim
string = ltrim(string)
http://php.net/manual/en/function.ltrim.php
*/
$text   = "\x0B\n\t\r\0 1 2 3 4 5 ";
var_dump($text); // 16
var_dump(ltrim($text)); // 10

/*
rtrim, chop
string = rtrim(string)
string = chop(string)
http://php.net/manual/en/function.rtrim.php
*/
$text   = "\x0B\n\t\r\0 1 2 3 4 5 ";
var_dump($text); // 16
var_dump(rtrim($text)); // 15
var_dump(chop($text)); // 15

/*
quotemeta
string = quotemeta(string)
http://php.net/manual/en/function.quotemeta.php
*/
$text   = " START . \ + * ? [ ^ ] ( $ ) END ";
echo $text; // START . \ + * ? [ ^ ] ( $ ) END
echo quotemeta($text); // START \. \\ \+ \* \? \[ \^ \] \( \$ \) END

/*
ord
int = ord(string)
http://php.net/manual/en/function.ord.php
*/
$ASCII = "A";
echo ord($ASCII); // 65

/*
chr
string = chr(int ascii)
http://php.net/manual/en/function.chr.php
*/
$ASCII = "65";
echo chr($ASCII); // A

/*
parse_str
void = parse_str(string, array)
http://php.net/manual/en/function.parse-str.php
*/
$str = "id=a&arr[]=b+c&arr[]=d";
parse_str($str);
echo $id;  // a
echo $arr[0]; // b c
echo $arr[1]; // d
parse_str($str, $output);
echo $output["id"];  // a
echo $output["arr"][0]; // b c
echo $output["arr"][1]; // d

/*
strip_tags
string = strip_tags(string, string)
http://php.net/manual/en/function.strip-tags.php
*/
$html = "The  Quick Brown Fox  Jumps Over The Lazy Dog. ";
echo strip_tags($html);
echo strip_tags($html,"<b>");

/*
str_repeat
string = str_repeat(string, int)
http://php.net/manual/en/function.str-repeat.php
*/
echo str_repeat("*", 10); // **********

/*
str_pad
string = str_pad(string, int, string, int)
http://php.net/manual/en/function.str-pad.php
*/
$str = "i";
echo str_pad($str, 5);                     // i
echo str_pad($str, 5, "*");                // i****
echo str_pad($str, 5, "*", STR_PAD_RIGHT); // i****
echo str_pad($str, 5, "*", STR_PAD_LEFT);  // ****i
echo str_pad($str, 5, "*", STR_PAD_BOTH);  // **i**

/*
substr_count
int = substr_count(string, string, int, int)
http://php.net/manual/en/function.substr-count.php
*/
$text = 'This is a test';
echo strlen($text); // 14
echo substr_count($text, 'is'); // 2 : This is a test
echo substr_count($text, 'is', 3); // 1 : s is a test
echo substr_count($text, 'is', 3, 3); // 0 : s i
echo substr_count($text, 'is', 5, 10); // error : 5+10 > 14

/*
crypt
string = crypt(string, salt)
http://php.net/manual/en/function.crypt.php
*/
$salt = "ab";
$password = "1234";
echo crypt($password);
echo crypt($password,$salt);

if (CRYPT_STD_DES == 1)
{
    echo "Standard DES: " . crypt("1234", "rl"); // 2 digits salt
}

if (CRYPT_EXT_DES == 1)
{
    echo "Extended DES: " . crypt("1234", "_J9..rasm"); // 9 digits salt
}

if (CRYPT_MD5 == 1)
{
    echo "MD5: " . crypt("1234", "$1$rasmusle$"); // 12 digits salt starting with $1$
}

if (CRYPT_BLOWFISH == 1)
{
    echo "Blowfish: " . crypt("1234", "$2a$07$usesomesillystringforsalt$"); // 22 character salt starting with $2$ | $2a$ | $2x$ | $2y$  
}

if (CRYPT_SHA256 == 1)
{
    echo "SHA-256: " . crypt("1234", "$5$rounds=5000$usesomesillystringforsalt$"); // 16 character salt starting with $5$ and number
}

if (CRYPT_SHA512 == 1)
{
    echo "SHA-512: " . crypt("1234", "$6$rounds=5000$usesomesillystringforsalt$"); // 16 character salt starting with $6$ and number
}

/*
htmlspecialchars
string = htmlspecialchars(string, style)
http://php.net/manual/en/function.htmlspecialchars.php
& : &
" : "
' : '
< : <
> : >

- flags 
ENT_COMPAT
ENT_QUOTES
ENT_NOQUOTES
ENT_IGNORE
ENT_SUBTITLE
ENT_DISALLOWED
ENT_HTML401
ENT_XML1
ENT_XHTML
ENT_HTML5

- encoding
ISO-8859-1
ISO-8859-5
ISO-8859-15
UTF-8
cp866
cp1251
KOI8-R
BIG5
GB2312
BIG5-HKSCS
Shift-JIS
EUC-JP
MacRoman
"
*/
$html = htmlspecialchars("Test", ENT_QUOTES);
echo $html; // <a href='test'>Test</a>

/*
htmlentities
string = htmlentities(string, flags, encoding)
http://php.net/manual/en/function.htmlentities.php
*/
$str = "\x8F!!!";
echo htmlentities($str, ENT_QUOTES, "UTF-8"); //
echo htmlentities($str, ENT_QUOTES | ENT_IGNORE, "UTF-8"); // !!!

/*
number_format
string = number_format(float, int, string, string)
http://php.net/manual/en/function.number-format.php
*/
$number = 1234.5678;
echo number_format($number); // 1,235
echo number_format($number, 2); // 1,234.57
echo number_format($number, 2, " point ", " and "); // 1 and 234 point 57
echo number_format($number, 2, ".", ""); // 1234.57

댓글 없음:

댓글 쓰기

플러터 단축키

1. 위젯 감싸기/벗기기 비주얼 스튜디오 :   Cmd + . 안드로이드 스튜디오 : Alt + Enter 2. 코드 정렬 비주얼 스튜디오 : Ctrl + S 안드로이드 스튜디오 : Ctlr + Alt + L 3. StatelessWidget ->...