4. Strings in PHPStrings in PHP
A string is an array of character.
$a = strtoupper($name); // büyük harf
$a = strtolower($name); // küçük harf
$a = ucfirst($name); // İlk karakterbüyük
$text = "A very long woooooooooooord.";
$newtext = wordwrap($text, 8, "n", 1);
$a = crpyt($a); // şifreleme
$a = decrypt(encrpyt($a)); // 2-way encription
with Mcrpt extension
5. Strings in PHPStrings in PHP
Slash ekler- (veritabanına eklerken)
$a = AddSlashes($typedText);
Slashları kaldırır
$a = StripSlashes($typedText);
8. Strings in PHPStrings in PHP
• string substr (string string, int start [, int length])
• int strlen (string str)
• int strcmp (string str1, string str2) Returns
o < 0 if str1 is less than str2;
o > 0 if str1 is greater than str2,
o 0 if they are equal.
9. Regular ExpressionsRegular Expressions
• A way of describing a pattern in string
• Use special characters to indicate meta-meaning in
addition to exact matching.
• More powerful than exact matching
• There are 2 sets of function on regular expressions in
PHP
o Functions using POSIX-type reg expr
o Functions using Perl-type reg expr
10. Regular ExpressionsRegular Expressions
“.” tek bir karakterle eşleşir
.at == “cat”, “sat”, etc.
[a-zA-Z0-9] tek bir karakterle (a-zA-Z0-9)
arasında eşleşir.
[^0-9] rakam olmayan birşeyle eşleşir.
12. Regular ExprRegular Expr
• . Tek karakter
• + 1 ya da daha fazla bulunan stringle
• * 0 ya da daha fazla bulunan stringle
• [a-z] karakter
• ^ değil anlamında
• $ string sonu
• | or
• özel karakterleri atlar
• (sub-expr) -- sub-expression
• (sub-expr){i,j} i min i, max j ile sub-expr olma durumu
13. Reg Expr Functions (Perl)Reg Expr Functions (Perl)
• preg_match — Perform a reg expr match
• preg_match_all — Perform a global r.e. match
• preg_replace — Perform a re search & replace
• preg_split — Split string by a reg expr
14. preg_match -- Perform apreg_match -- Perform a
re matchre match
int preg_match (string pattern, string subject [, array
matches])
• Searches subject for a match to the reg expr given
in pattern.
• Return one match for each subpattern () only
• $matches[0]: the text matching the full pattern
• $matches[1]: the text that matched the first
captured parenthesized subpattern, and so on.
• Returns true if a match for pattern was found
15. preg_match -- Perform apreg_match -- Perform a
re matchre match
preg_match("/pattern/modifier", subject, array)
Modifiers:
• i: case insensitive search
• m: by default subject is treated single-line even if it contains
newlines, m makes PCRE treat subject multiline (for ^, $)
• s: makes . metacharacter match n
• x: whitespace in pattern is ignored
• E: $ matches only at the end of subject
• U: behave ungreedy (comert)
16. preg_match -- Perform apreg_match -- Perform a
re matchre match$s = <<<STR
<table><tr><td>cell1</td><td>cell2</td></tr>
<tr><td>cell3</td><td>cell4</td></tr></table>
STR;
preg_match("/<table>(.*)</table>/Us", $s, $r)
// anything between <table> and </table>
preg_match("/<tr><td>(.*)</td><td>(.*)</td></tr>/Us", $r[1],
$t)
// matches cell1 and cell2
preg_match("/<tr>(.*)</tr>/Us", $r[1], $t);
// matches <td>cell1</td><td>cell2</td>
17. preg_matchpreg_match_all:_all: Perform Perform globalglobal matchmatch
int preg_match_all (string pattern, string subject, array
matches [, int order])
• Searches subject for all matches and puts them in
matches in the order specified by order.
• After the first match, the subsequent ones are
continued on from end of the last match.
• $matches[0] is an array of full pattern matches
• $matches[1] is an array of strings matched by the
first parenthesized subpattern, and so on.
18. preg_matchpreg_match_all:_all: Perform Perform globalglobal matchmatch
preg_match("/<table>(.*)</table>/Us", $s, $r);
preg_match_all("/<tr><td>(.*)</td><td>(.*)</td></
tr>/Us", $r[1], $t);
echo $t[1][0],$t[1][1],$t[2][0],$t[2][1];
// prints cell1cell3cell2cell4
preg_match_all("/<tr><td>(.*)</td><td>(.*)</td></
tr>/Us", $r[1], $t, PREG_SET_ORDER );
//Orders results so that $matches[0] is an array of first
set of matches, $matches[1] is an array of second
set of matches,…
echo $t[0][1],$t[0][2],$t[1][1],$t[1][2];
// returns cell1cell2cell3cell4
20. Convert HTML to TextConvert HTML to Text
$html = file(“http://www.page.com”);
$search = array ("'<script[^>]*?>.*?</script>'si",
"'<[/!]*?[^<>]*?>'si",
"'([rn])[s]+'",
"'&(quote|#34);'i",
"'&(amp|#38);'i", …);
$replace = array ("", "", "1", """, "&", …);
$text = preg_replace ($search, $replace, $html);
21. Reg Expr FunctionsReg Expr Functions
(POSIX)(POSIX)
• ereg (string pattern, string string [, array regs])
o Searches a string for matches to the regular expression given in pattern.
• ereg_replace (string pattern, string subs, string string)
o Scans string for matches to pattern, then replaces the matched text with
subs.
• array split (string pattern, string string [, int limit])
o Split string using pattern