1 php intro php strings after this lecture, you should be able to: manipulate and output php...

17
1 PHP Intro PHP Intro PHP Strings PHP Strings After this lecture, you should After this lecture, you should be able to: be able to: Manipulate and Output Manipulate and Output PHP PHP Strings Strings : : Single- Single- or or Double-quoted Double-quoted strings strings String String Conversion Conversion String String Functions Functions

Upload: james-lawson

Post on 13-Jan-2016

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 PHP Intro PHP Strings After this lecture, you should be able to: Manipulate and Output PHP Strings: Manipulate and Output PHP Strings: Single- or Double-quoted

11PHP IntroPHP Intro

PHP StringsPHP Strings

After this lecture, you should be able After this lecture, you should be able to:to:

Manipulate and Output Manipulate and Output PHP StringsPHP Strings:: Single-Single- or or Double-quotedDouble-quoted strings strings String String ConversionConversion String String FunctionsFunctions

Page 2: 1 PHP Intro PHP Strings After this lecture, you should be able to: Manipulate and Output PHP Strings: Manipulate and Output PHP Strings: Single- or Double-quoted

22PHP StringPHP String

PHP StringsPHP Strings

A string can be A string can be single-single- or or double-quoteddouble-quoted.. An extensive set of functions for strings are An extensive set of functions for strings are

provided:provided: Manipulation with a Manipulation with a regular expressionregular expression,, ConversionConversion between a string and an between a string and an

array,array, Outputing by Outputing by echo()echo(),, print() print(),, printf() printf(), ,

and and print_r()print_r(), and, and OthersOthers

Page 3: 1 PHP Intro PHP Strings After this lecture, you should be able to: Manipulate and Output PHP Strings: Manipulate and Output PHP Strings: Single- or Double-quoted

33PHP StringPHP String

Single-Quoted StringSingle-Quoted String

No evaluationNo evaluation occurs for a single-quoted string. occurs for a single-quoted string.

$age = 20;$age = 20;echo 'I am $age.\n';echo 'I am $age.\n';echo ''Single'';echo ''Single'';echo '\”Double\”';echo '\”Double\”';echo '”Double”';echo '”Double”';echo $age >= 18 ? 'OK' : '';echo $age >= 18 ? 'OK' : '';

I am $age.\n(illegal)\“Double\”“Double”OK

Page 4: 1 PHP Intro PHP Strings After this lecture, you should be able to: Manipulate and Output PHP Strings: Manipulate and Output PHP Strings: Single- or Double-quoted

44PHP StringPHP String

Double-Quoted StringDouble-Quoted String

$age = 20;$age = 20;echo “I am $age.\n”;echo “I am $age.\n”;echo “'Single'\n”;echo “'Single'\n”;echo “\”Double\”\n”;echo “\”Double\”\n”;echo “”Double””;echo “”Double””;echo $age >= 18 ? “OK” : “”;echo $age >= 18 ? “OK” : “”;

Variables and escape sequences within a double-Variables and escape sequences within a double-quotedquoted

string are string are evaluatedevaluated or or replacedreplaced..

I am 20.'Single'“Double”(illegal)OK

Page 5: 1 PHP Intro PHP Strings After this lecture, you should be able to: Manipulate and Output PHP Strings: Manipulate and Output PHP Strings: Single- or Double-quoted

55PHP StringPHP String

int strlen(string)int strlen(string)

$str = 'Hello';$str = 'Hello';

$len = strlen($str);$len = strlen($str);

echo $len;echo $len;

Returns the length of stringReturns the length of string stringstring..

5

Page 6: 1 PHP Intro PHP Strings After this lecture, you should be able to: Manipulate and Output PHP Strings: Manipulate and Output PHP Strings: Single- or Double-quoted

66PHP StringPHP String

int strpos(string1, string2)int strpos(string1, string2)

$str = 'My dog house.';$str = 'My dog house.';

$position = strpos($str, 'dog');$position = strpos($str, 'dog');

echo $position;echo $position;

Returns the numeric position of Returns the numeric position of string2string2 in in

string1string1..

3

Page 7: 1 PHP Intro PHP Strings After this lecture, you should be able to: Manipulate and Output PHP Strings: Manipulate and Output PHP Strings: Single- or Double-quoted

77PHP StringPHP String

int strpos($str1, $str2 ): int strpos($str1, $str2 ): WarningWarning

Returns Returns falsefalse if if $str2$str2 is not a substring of is not a substring of $str1$str1..

Returns Returns 00 if if $str2$str2 is at the beginning of is at the beginning of $str1$str1, , so using so using if(strpos($str1, $str2) == 0)if(strpos($str1, $str2) == 0) could cause an unreliable result. could cause an unreliable result.

if(strpos($str1, $str2) === 0) may be if(strpos($str1, $str2) === 0) may be used.used.

Page 8: 1 PHP Intro PHP Strings After this lecture, you should be able to: Manipulate and Output PHP Strings: Manipulate and Output PHP Strings: Single- or Double-quoted

88PHP StringPHP String

string string substrsubstr(string, start[, length](string, start[, length]))

SubstringSubstring

$str = “My dog.”;$str = “My dog.”;

$str2 = substr($str, 3);$str2 = substr($str, 3);

echo $str2echo $str2

$str3 = substr($str1, 3, 2);$str3 = substr($str1, 3, 2);

echo $str3echo $str3

dog.

do

Returns the string present in Returns the string present in string string starting starting fromfrom

location location startstart until end of until end of stringstring. .

Page 9: 1 PHP Intro PHP Strings After this lecture, you should be able to: Manipulate and Output PHP Strings: Manipulate and Output PHP Strings: Single- or Double-quoted

99PHP StringPHP String

array explode(delim, string):array explode(delim, string):

$str = “Kelley Engineering Center”;$str = “Kelley Engineering Center”;

$arr = explode(" ", $str);$arr = explode(" ", $str);

echo “$arr[0]\n$arr[1]\n$arr[2]\n”;echo “$arr[0]\n$arr[1]\n$arr[2]\n”;

Creates an array of the substrings in Creates an array of the substrings in

stringstring,,

separated by separated by delimdelim..

KelleyEngineeringCenter

Page 10: 1 PHP Intro PHP Strings After this lecture, you should be able to: Manipulate and Output PHP Strings: Manipulate and Output PHP Strings: Single- or Double-quoted

1010PHP StringPHP String

string implode(delim, array):string implode(delim, array):

$arr = array('S10', 'Wolf', '20');$arr = array('S10', 'Wolf', '20');

$string = implode('|', $arr);$string = implode('|', $arr);

echo $stringecho $string

Creates a string out of the elements in Creates a string out of the elements in

arrayarray,,

connected by connected by delimdelim..

S10|Wolf|20

Page 11: 1 PHP Intro PHP Strings After this lecture, you should be able to: Manipulate and Output PHP Strings: Manipulate and Output PHP Strings: Single- or Double-quoted

1111PHP StringPHP String

int ereg(pattern, string[, array]): int ereg(pattern, string[, array]): Pattern Matching IPattern Matching I

ereg('boy', 'cowboys');ereg('boy', 'cowboys');ereg('^boy', 'cowboys');ereg('^boy', 'cowboys');ereg('^cow', 'cowboys');ereg('^cow', 'cowboys');ereg('cow$', 'cowboys');ereg('cow$', 'cowboys');ereg('boys$', 'cowboys');ereg('boys$', 'cowboys');

Tests whether or not Tests whether or not stringstring matches matches regularregular

expression expression patternpattern..truetruefalsefalsetruetruefalsefalsetruetrue

This function has been DEPRECATED as of PHP 5.3.0. This function has been DEPRECATED as of PHP 5.3.0.

preg_match()preg_match() is an alternative. is an alternative.

Page 12: 1 PHP Intro PHP Strings After this lecture, you should be able to: Manipulate and Output PHP Strings: Manipulate and Output PHP Strings: Single- or Double-quoted

1212PHP StringPHP String

Meta Characters for a Regular Meta Characters for a Regular ExpressionExpression

^ - start of line, or negation of^ - start of line, or negation of characterscharacters$ - end of line$ - end of line. - any character. - any character[] – range, or set of characters[] – range, or set of characters() – grouping() – grouping* - any number of times* - any number of times+ - at least once+ - at least once? - exactly once? - exactly once{n, m} – match n through m occurrences{n, m} – match n through m occurrences

The following meta characters can be used in The following meta characters can be used in aa

regular expression.regular expression.

Page 13: 1 PHP Intro PHP Strings After this lecture, you should be able to: Manipulate and Output PHP Strings: Manipulate and Output PHP Strings: Single- or Double-quoted

1313PHP StringPHP String

int ereg(pattern, string[, array]): int ereg(pattern, string[, array]): Pattern Matching IIPattern Matching II

ereg('.*', 'cowboys'); ereg('.*', 'cowboys'); ereg('. . . . . . .', 'cowboys');ereg('. . . . . . .', 'cowboys');ereg('c.*s', 'cowboys');ereg('c.*s', 'cowboys');ereg('c.*s', 'cowgirls'); ereg('c.*s', 'cowgirls'); ereg('c.*s', 'tomboys'); ereg('c.*s', 'tomboys'); ereg('^a*b*$', 'aaabb'); ereg('^a*b*$', 'aaabb'); ereg('^a*b*$', 'bbbb'); ereg('^a*b*$', 'bbbb'); ereg('^a*b*$', 'ccbbb'); ereg('^a*b*$', 'ccbbb'); ereg('^a+b*$', 'aabbb'); ereg('^a+b*$', 'aabbb'); ereg('^a+b*$', 'bbb');ereg('^a+b*$', 'bbb');

Wild card characters.Wild card characters.

truetruetruetruetruetruetruetruefalsefalsetruetruetruetruefalsefalsetruetruefalsefalse

Page 14: 1 PHP Intro PHP Strings After this lecture, you should be able to: Manipulate and Output PHP Strings: Manipulate and Output PHP Strings: Single- or Double-quoted

1414PHP StringPHP String

int ereg(pattern, string[, array]): int ereg(pattern, string[, array]): Pattern Matching IIIPattern Matching III

ereg('^[a-z]*$', 'cowboy');ereg('^[a-z]*$', 'cowboy');ereg('^[a-z]*$', 'cowboy123');ereg('^[a-z]*$', 'cowboy123');ereg('^[a-z]*[0-9]*$', 'cowboy123');ereg('^[a-z]*[0-9]*$', 'cowboy123');ereg('^[a-z]{6}$', 'cowboy');ereg('^[a-z]{6}$', 'cowboy');ereg('^[a-z]{4}$', 'cowboy');ereg('^[a-z]{4}$', 'cowboy');ereg('^[bcowy]*$', 'cowboy');ereg('^[bcowy]*$', 'cowboy');ereg('^[^0-9]$', 'a');ereg('^[^0-9]$', 'a');

Specifying a range or set of valid characters.Specifying a range or set of valid characters.

truetruefalsefalsetruetruetruetruefalsefalsetruetruetruetrue

Page 15: 1 PHP Intro PHP Strings After this lecture, you should be able to: Manipulate and Output PHP Strings: Manipulate and Output PHP Strings: Single- or Double-quoted

1515PHP StringPHP String

int ereg(pattern, string[, array]): int ereg(pattern, string[, array]): Extracting SubstringsExtracting Substrings

$email = “[email protected]”;$email = “[email protected]”;ereg('(.*)@(.*)', $email, $arr);ereg('(.*)@(.*)', $email, $arr);

echo “User: $arr[1]\n”;echo “User: $arr[1]\n”;echo “Host: $arr[2]\n”;echo “Host: $arr[2]\n”;echo “Email: $arr[0]\n”;echo “Email: $arr[0]\n”;

User: phamHost: eecs.oregonstate.eduEmail: [email protected]

Retrieving text in groups.Retrieving text in groups.

Page 16: 1 PHP Intro PHP Strings After this lecture, you should be able to: Manipulate and Output PHP Strings: Manipulate and Output PHP Strings: Single- or Double-quoted

1616PHP StringPHP String

Meta Characters for a Regular Meta Characters for a Regular ExpressionExpression

^ - start of line, or negation of^ - start of line, or negation of characterscharacters$ - end of line$ - end of line. - any character. - any character[] – range, or set of characters[] – range, or set of characters() – grouping() – grouping* - any number of times* - any number of times+ - at least once+ - at least once? - exactly once? - exactly once{n, m} – match n through m occurrences{n, m} – match n through m occurrences

The following meta characters can be used in The following meta characters can be used in aa

regular expression.regular expression.

Page 17: 1 PHP Intro PHP Strings After this lecture, you should be able to: Manipulate and Output PHP Strings: Manipulate and Output PHP Strings: Single- or Double-quoted

1717

Example: is_safe_int()<?php

function is_safe_int($var) { $pattern = "/^0$|^[1-9][0-9]*$/"; if(preg_match($pattern, $var)) { return true; } else { return false; } }?>