TIL/PHP
[PHP] πλ¬Έμμ΄&μ κ·μ
hannaπ€
2022. 1. 30. 12:09
728x90
# λ¬Έμμ΄ μ²λ¦¬
λ¬Έμμ΄(string)
- ''(λ¨λ°μ΄ν), ""(μλ°μ΄ν)λ₯Ό μ¬μ©ν΄μ νκΈ°νλ€.
echo 'hello', echo "hello" - ''(λ¨λ°μ΄ν)λ ""(μλ°μ΄ν)λ₯Ό νκΈ°ν λλ \(μμ¬λμ¬)λ₯Ό μ¬μ©νλ€.
echo 'hello\'world\'', echo "hello\"world\"" - μ€λ°κΏ ν λλ μλ°μ΄ν μμμ \n. \rμ μ¬μ©νλ€.
echo "hello \n" - λ¬Έμμ΄ μμμ λ³μλ₯Ό μ¬μ©νλ €λ©΄ μλ°μ΄ν μμμ {, }(μ€κ΄νΈ)λ₯Ό μ¬μ©νλ€.
<? $a = array('hello', 'world'); echo "\'λ³μ리κ°λ°μ\'λΈλ‘κ·Έμ 곡μμΈμ¬λ {$a[0]} {$a[1]}μ λλ€."; echo '\"λ³μ리κ°λ°μ\"λΈλ‘κ·Έμ 곡μμΈμ¬λ '.$a[0].' '.$a[1].'μ λλ€.'; ?>
- λ¬Έμμ λ¬Έμλ₯Ό λν λλ '.'(λ§μΉ¨ν)λ₯Ό μ¬μ©νλ€.
echo 'hello'.'world'
λ¬Έμμ κ΄λ ¨λ μ£Όμ ν¨μ
# μ κ·ννμ
- λ¬Έμμ΄μ μ²λ¦¬νλ λ°©λ²μ€μ νλμ΄λ€.
- νΉμ ν 쑰건μ λ¬Έμλ₯Ό 'κ²μ', 'μΉν'νλ κ³Όμ μ λ§€μ° κ°νΈνκ² μ²λ¦¬ν μ μλλ‘ νλ μλ¨μ΄λ€.
- ꡬλΆμλ‘ μμν΄μ ꡬλΆμλ‘ λμ λ΄μΌνλ€.
ꡬλΆμ: /, #, μνλ²³, \, κ³΅λ°±μ΄ μλ λ¬Έμ - λμλ¬Έμλ₯Ό ꡬλΆνμ§ μλ i, μ€λ°κΏ λ¬Έμμ λ°λΌμ ν
μ€νΈμ νμ ꡬλΆνλλ‘ νλ m
ex) /foo bar/im
\b
- λ¨μ΄μ κ²½κ³λ₯Ό μλ―Έ
<? if(preg_match("/\bweb\b/i", "PHP is the web scripting language of chlice.")) { echo "A match was found."; } else { echo "A match was not found."; } /* μ€νκ²°κ³Ό - webμ΄ λ¨λ μΌλ‘ μ°μκΈ° λλ¬Έμ 쑰건 μΆ©μ‘± A match was found. */ if(preg_match("/\bweb\b/i", "PHP is the website scripting language of choice.")) { echo "A match was found."; } else { echo "A match was not found."; } /* μ€νκ²°κ³Ό - webμ΄ λ¨λ μΌλ‘ μ°μ΄μ§ μμκΈ° λλ¬Έμ 쑰건 λΆμΆ©μ‘± A match was not found. */ ?>
\w
- λ¬Έμλ₯Ό μλ―Έ
<? $subject = 'coding every body http:// opentutorials.org egoing@egoing.com 010-0000-0000'; /* $matchλ³μλ μμ λ°λ‘ μ μΈνμ§ μμλλ¨ $matchλ³μμ κ²μν κ²°κ³Όκ° μ μ₯λ¨ '/'λ₯Ό κ²μνκ³ μΆλ€λ©΄ '\/'λ‘ νκ±°λ ꡬλΆμλ₯Ό λ€λ₯Έκ±°λ‘ λ°κΎΌλ€. +: λ¬Έμκ° 1κ° μ΄μμ κ²μ .: μ΄λ ν λ¬Έμ건 μκ΄μμ΄ .μ΄ μμΉν κ³³μ λ±μ₯ (any character) \s: 곡백μ μλ―Έν¨ */ preg_match('~(http://\w+\.\w+)\s(\w+@\w+\.\w+)~', $subject, $match); var_dump($match); /* var_dump($match)μ μ€νκ²°κ³Ό array(3) { [0]=> string(42) "http://oentutorials.org egoing@egoing.com" [1]=> string(24) "http://opentutorials.org" [2]=> string(17) "egoing@egoing.com" } */ echo "homepage:".$match[1]; echo"<br/>"; echo "email:".$match[2]; ?>
μμ
- URLμμ λλ©μΈμ μ΄λ¦ μΆμΆνκΈ°(php.netμ μΆλ ₯)
<? preg_match('@^(?:http://)?([^/]+)@i', "http://www.php.nex/index.html", $matches); $host = $matches[1]; // κ²°κ³Όκ°: www.php.net ?>
- μ κ·μ ν΄μ€
@: ꡬλΆμ
^: λ¬Έμμ΄μ μ²μμ ν΄λΉνλ κ²½κ³, httpλ‘ μμνλ λ¨μ΄ μΆμΆ
?:: ()μμ μλ λ΄μ©μ $matchs λ³μμ λ€μ΄κ°μ§ μλλ€.- http://λ $matchs λ³μμ λ€μ΄κ°μ§ μλλ€.
[^/]: /κ° μλ λ¬Έμλ€(μ 체)
[](λκ΄νΈ)μμ ^: λΆμ μ μλ―Έν¨
- μ κ·μ ν΄μ€
μΆμ²: https://opentutorials.org/index.php/course/62
PHP κΈ°λ³Έ μμ - μνμ½λ©
κ°μ PHPλ μλ² μΈ‘μμ μ€νλλ νλ‘κ·Έλλ° μΈμ΄λ‘ HTMLμ νλ‘κ·Έλλ°μ μΌλ‘ μμ±ν΄μ£Όκ³ , λ°μ΄ν°λ² μ΄μ€μ μνΈμμ© νλ©΄μ λ°μ΄ν°λ₯Ό μ μ₯νκ³ , ννν©λλ€. PHPλ μΉμ μν΄μ λ§λ€μ΄μ‘κ³ , μ§κΈ
opentutorials.org
728x90