Presentation is loading. Please wait.

Presentation is loading. Please wait.

STRINGler. Stringler $a = trim($name);//kırpma $a = nl2br(“line1\nline2”); // “line1 line2” çevirir printf(“total %d”, 15);// prints to stdout.

Similar presentations


Presentation on theme: "STRINGler. Stringler $a = trim($name);//kırpma $a = nl2br(“line1\nline2”); // “line1 line2” çevirir printf(“total %d”, 15);// prints to stdout."— Presentation transcript:

1 STRINGler

2 Stringler $a = trim($name);//kırpma $a = nl2br(“line1\nline2”); // “line1 line2” çevirir printf(“total %d”, 15);// prints to stdout

3 URL olarak string gönderme $name = urlencode ($name); $email = urlencode ($_POST['email']); print "Click here to continue.";

4 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

5 Strings in PHP Slash ekler- (veritabanına eklerken) $a = AddSlashes($typedText); Slashları kaldırır $a = StripSlashes($typedText);

6 String birleştirme ve ayırma $pieces[$i] "; echo implode(“:", $pieces);// join strings using “:“ ?>

7 Stringleri ayırma $string = "This is an example string"; $tok = strtok ($string," "); while ($tok) { echo "Word=$tok "; $tok = strtok (" "); }

8 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.

9 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

10 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.

11 Regular Expr: Built-in Char-sets [[:alphanum:]]--harf [[:digit:]]rakamla [[:space:]] boşlukla

12 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

13 Reg Expr Functions (Perl) preg_match — Perform a reg expr match preg_match preg_match_all — Perform a global r.e. match preg_match_all preg_replace — Perform a re search & replace preg_replace preg_split — Split string by a reg expr preg_split

14 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

15 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)

16 preg_match -- Perform a re match $s = << { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/8/2451696/slides/slide_16.jpg", "name": "preg_match -- Perform a re match $s = <<

17 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.

18 preg_match_all: Perform global match preg_match("/ (.*) /Us", $s, $r); preg_match_all("/ (.*) (.*) /Us", $r[1], $t); echo $t[1][0],$t[1][1],$t[2][0],$t[2][1]; // prints cell1cell3cell2cell4 preg_match_all("/ (.*) (.*) /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

19 preg_match_all: Perform global match Back reference preg_match_all ("/( ]*>)(.*)( )/", $html, $matches); // \\2 means [\w]+\\2 preg_match_all ("| ]+>(.*) ]+>|U", $html, $m)// return text in html tags

20 Convert HTML to Text $html = file(“http://www.page.com”); $search = array ("' ]*?>.*? 'si", "' ]*?>'si", "'([\r\n])[\s]+'", "'&(quote|#34);'i", "'&(amp|#38);'i", …); $replace = array ("", "", "\\1", "\"", "&", …); $text = preg_replace ($search, $replace, $html);

21 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

22 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 ("^", " ", $string); $string = ereg_replace ("$", " ", $string); $string = ereg_replace ("\n", "", $string);

23 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"; }

24 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);

25 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";


Download ppt "STRINGler. Stringler $a = trim($name);//kırpma $a = nl2br(“line1\nline2”); // “line1 line2” çevirir printf(“total %d”, 15);// prints to stdout."

Similar presentations


Ads by Google