Upload
ashlyn-spurgeon
View
216
Download
0
Embed Size (px)
Citation preview
STRINGler
Stringler
$a = trim($name); //kırpma
$a = nl2br(“line1\nline2”); // “line1<br>line2” çevirir
printf(“total %d”, 15); // prints to stdout
URL olarak string gönderme
$name = urlencode ($name);
$email = urlencode ($_POST['email']);
print "Click <a href=\"thanks.php?name=$name&email=$email\">here</a> to continue.";
Strings 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
Strings in PHP
Slash ekler- (veritabanına eklerken)
$a = AddSlashes($typedText);
Slashları kaldırır
$a = StripSlashes($typedText);
String birleştirme ve ayırma
<?$pizza = "piece1 piece2 piece3 piece4
piece5";$pieces = explode (" ", $pizza); // split string
into piecesfor($i=0; $i<count($pieces); $i++)
echo "-> $pieces[$i]<br>";echo implode(“:", $pieces); // join
strings using “:“ ?>
Stringleri ayırma
$string = "This is an example string";
$tok = strtok ($string," ");
while ($tok) {
echo "Word=$tok<br>";
$tok = strtok (" ");
}
Strings in PHP
string substr (string string, int start [, int length]) int strlen (string str) int strcmp (string str1, string str2) Returns
– < 0 if str1 is less than str2; – > 0 if str1 is greater than str2,– 0 if they are equal.
Regular 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– Functions using POSIX-type reg expr– Functions using Perl-type reg expr
Regular 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.
Regular Expr: Built-in Char-sets
[[:alphanum:]] --harf [[:digit:]] rakamla [[:space:]] boşlukla
Regular 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
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
preg_match -- Perform a re 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
preg_match -- Perform a re 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)
preg_match -- Perform a re 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 cell2preg_match("/<tr>(.*)<\/tr>/Us", $r[1], $t);// matches <td>cell1</td><td>cell2</td>
preg_match_all: Perform global match
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.
preg_match_all: Perform global match
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 cell1cell3cell2cell4preg_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
preg_match_all: Perform global match
Back reference
preg_match_all ("/(<([\w]+)[^>]*>)(.*)(<\/\\2>)/", $html, $matches);
// \\2 means [\w]+
preg_match_all ("|<[^>]+>(.*)</[^>]+>|U", $html, $m) // return text in html tags
Convert HTML to Text
$html = file(“http://www.page.com”);
$search = array ("'<script[^>]*?>.*?</script>'si", "'<[\/\!]*?[^<>]*?>'si",
"'([\r\n])[\s]+'",
"'&(quote|#34);'i",
"'&(amp|#38);'i", …);
$replace = array ("", "", "\\1", "\"", "&", …);
$text = preg_replace ($search, $replace, $html);
Reg Expr Functions (POSIX)
ereg (string pattern, string string [, array regs])– Searches a string for matches to the regular
expression given in pattern.
ereg_replace (string pattern, string subs, string string)– Scans string for matches to pattern, then replaces
the matched text with subs.
array split (string pattern, string string [, int limit])– Split string using pattern
Regular Expr
ereg ("abc", $string); ereg ("^abc", $string); ereg ("abc$", $string);eregi ("(ozilla.[23]|MSIE.3)",
$HTTP_USER_AGENT); ereg ("([[:alnum:]]+) ([[:alnum:]]+) ([[:alnum:]]+)",
$string,$regs);$string = ereg_replace ("^", "<BR>", $string);$string = ereg_replace ("$", "<BR>", $string);$string = ereg_replace ("\n", "", $string);
Regular Expr
if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $date, $regs)) {
echo "$regs[3].$regs[2].$regs[1]";
}else{
echo "Invalid date format: $date";
}
Regular Expr
$string = "This is a test";
echo ereg_replace (" is", " was", $string);
echo ereg_replace ("( )is", "\\1was", $string);
echo ereg_replace ("(( )is)", "\\2was", $string);
Regular Expr
$date = "04/30/1973";
// Delimiters may be slash, dot, or hyphen list
($month, $day, $year) = split ('[/.-]', $date);
echo "Month: $month; Day: $day; Year: $year";