33
Strings

Intoduction to php strings

Embed Size (px)

Citation preview

Page 1: Intoduction to php  strings

Strings

Page 2: Intoduction to php  strings

Strings

Char a*+=‚Baabtra‛;

Printf(‚Hello %s‛,a);

$a=‚Baabtra‛;

echo ‚Hello $a‛; //Output: Hello baabtra;

echo ‘Hello $a’; //Outputs : Hello $a

C PHP

Page 3: Intoduction to php  strings

String basics

1. Single quote strings (‘ ’)

– single quotes represent ‚simple strings,‛ where almost all characters are

used literally

2. Double quote strings(‚ ‛)

– complex strings‛ that allow for special escape sequences (for example, to

insert special characters) and for variable substitution

$a=‚Baabtra‛;

echo ‘Hello $a’;

echo ‚Hello $a \n welcome‛;

Hello $a

Hello baabtra

welcome

Out put

Page 4: Intoduction to php  strings

String basics - contd

• Clearly, this ‚simple‛ syntax won’t work in those situations in which the

name of the variable you want to interpolated is positioned in such a way

inside the string that the parser wouldn’t be able to parse its name in the

way you intend it to. In these cases, you can encapsulate the variable’s name

in braces

$me = ’Davey’;

$names = array (’Smith’, ’Jones’, ’Jackson’);

echo "There cannot be more than two {$me}s!";

echo "Citation: {$names[1]}[1987]";

Page 5: Intoduction to php  strings

String basics - contd

3. The Heredoc Syntax

used to declare complex strings, the functionality it provides is similar to double

quotes, with the exception that, because heredoc uses a special set of tokens to

encapsulate the string, it’s easier to declare strings that include many double-

quote characters.

$who = "World";

echo <<<TEXT

So I said, "Hello $who"

TEXT;

Page 6: Intoduction to php  strings

Escaping Literal Values

• All three string-definition syntax feature a set of several characters

that require escaping in order to be interpreted as literals.

echo ’This is \’my\’ string’;

$a = 10;

echo "The value of \$a is \"$a\".";

echo "Here’s an escaped backslash:\\";

Page 7: Intoduction to php  strings

String as arrays

• You can access the individual characters of a string as if they were

members of an array

$string = ’abcdef’;

echo $string[1]; // Outputs ’b’}

Page 8: Intoduction to php  strings

String comparison == and ===

$string = ’123aa’;

if ($string == 123) {

// The string equals 123

}

• You’d expect this comparison to return false, since the two operands are not the

same. However, PHP first transparently converts the contents of $string to the

integer 123, thus making the comparison true.

• Naturally, the best way to avoid this problem is to use the identity operator ===

Page 9: Intoduction to php  strings

String functions

Page 10: Intoduction to php  strings

String functions –strcmp(), strcasecmp()

strcmp(), strcasecmp() both returns zero if the two strings passed to the

function are equal. These are identical, with the exception that the former is

case-sensitive, while the latter is not.

$str = "Hello World";

if (strcmp($str, "hello world") === 0) {

// We won’t get here, because of case sensitivity

}

if (strcasecmp($str, "hello world") === 0) {

// We will get here, because strcasecmp() is case-insensitive

}

Page 11: Intoduction to php  strings

String functions –strcasencmp()

strcasencmp() allows you to only test a given number of characters

inside two strings.

$s1 = ’abcd1234’;

$s2 = ’abcd5678’;

// Compare the first four characters

echo strcasencmp ($s1, $s2, 4);

Page 12: Intoduction to php  strings

String functions –strlen()

strlen() is used to determine the length of a string

$a="baabtra mentoring parnter";

echo strlen($a);25

Out put

Page 13: Intoduction to php  strings

String functions –strtr()

strtr() used to translate certain characters of a string into other

characters

• Single character version

echo strstr (’abc’, ’a’, ’1’);

• Multiple-character version

$subst = array (’1’ => ’one’,’2’ => ’two’);

echo strtr (’123’, $subst);

1bc

Out put

onetwo3

Out put

Page 14: Intoduction to php  strings

String functions –strpos()

• strpos() allows you to find the position of a substring inside a string. It

returns either the numeric position of the substring’s first occurrence within

the string, or false if a match could not be found.

• You can also specify an optional third parameter to strpos() to indicate that you

want the search to start from a specific position within the haystack.

$haystack = ’123456123456’;

$needle = ’123’;

echo strpos ($haystack, $needle);

echo strpos ($haystack, $needle, 1);

0

Out put

6

Page 15: Intoduction to php  strings

String functions –stripos() , strrpos()

• stripos() is case-insensitive version of strpos()

echo stripos(’Hello World’, ’hello’);

• Strpos() does the same as strpos(), but in the revers order

echo strrpos (’123123’, ’123’);

0

Out put

3

Out put

Page 16: Intoduction to php  strings

String functions –strstr()• The strstr() function works similarly to strpos() in that it searches the main

string for a substring. The only real difference is that this function returns the

portion of the main string that starts with the sub string instead of the latter’s

position:

$haystack = ’123456’;

$needle = ’34’;

echo strstr ($haystack, $needle);

3456

Out put

Page 17: Intoduction to php  strings

String functions –stristr()

• stristr() is case-insensitive version of strstr()

echo stristr(’Hello My World’, ’my’); My World

Out put

Page 18: Intoduction to php  strings

String functions –str_replace(), str_ireplace()

• str_replace() used to replace portions of a string with a different substring

echo str_replace("World", "Reader", "Hello World");

• Str_ireplace() is the case insensitive version of str_replace()

echo str_ireplace("world", "Reader", "Hello World");

• Optionally, you can specify a third parameter, that the function fills, upon

return, with the number of substitutions made:

$a = 0;

str_replace (’a’, ’b’, ’a1a1a1’, $a);

echo $a;

Hello Reader

Out put

Hello Reader

Out put

3

Page 19: Intoduction to php  strings

String functions –str_replace(), str_ireplace()

• If you need to search and replace more than one needle at a time, you can pass the first

two arguments to str_replace() in the form of arrays

• echo str_replace(array("Hello", "World"), array("Bonjour", "Monde"), "HelloWorld");

• echo str_replace(array("Hello", "World"), "Bye", "Hello World");

Bye Bye

Hello Reader

Page 20: Intoduction to php  strings

String functions –substr()

• The very flexible and powerful substr() function allows you to extract

a substring from a larger string.

echo substr ($x, 0, 3); outputs 123

echo substr ($x, 1, 1); outputs 2

echo substr ($x, -2); outputs 67

echo substr ($x, 1); outputs 234567

echo substr ($x, -2, 1); outputs 6

Page 21: Intoduction to php  strings

String functions –number_format()

• Number formatting is typically used when you wish to output a

number and separate its digits into thousands and decimal points

• echo number_format("100000.698‚ , 3 , "," , ‚ ");

100 000,698

Number to be formatted

Number of decimal places to be rouned

decimal separator

Thousand separator

Output

Page 22: Intoduction to php  strings

Regular expressions

Page 23: Intoduction to php  strings

Regular Expressions

• Perl Compatible Regular Expressions (normally abbreviated as

‚PCRE‛) offer a very powerful string-matching and replacement

mechanism that far surpasses anything we have examined so far.

• The real power of regular expressions comes into play when you

don’t know the exact string that you want to match

Page 24: Intoduction to php  strings

Regular Expressions - Delimiters

• A regular expression is always delimited by a starting and ending

character.

• Any character can be used for this purpose (as long as the

beginning and ending delimiter match); since any occurrence of

this character inside the expression itself must be escaped, it’s

usually a good idea to pick a delimiter that isn’t likely to appear

inside the expression.

Page 25: Intoduction to php  strings

Regular Expressions – Meta characters

• However, every metacharacter represents a single character in the

matched expression.

. (dot)Match any character

ˆ Match the start of the string

$ Match the end of the string

\s Match any whitespace character

\d Match any digit

\w Match any ‚word‛ character

Page 26: Intoduction to php  strings

Regular Expressions – Meta characters

• Meta characters can also be expressed using grouping expressions. For example, a

series of valid alternatives for a character can be provided by using square brackets:

/ab[cd]e/

• You can also use other metacharacters, and provide ranges of valid characters inside a

grouping expression:

/ab[c-e\d]/

The expression will match abce or abde

This will match abc, abd, abe and any combination of ab followed by a digit.

Page 27: Intoduction to php  strings

Regular Expressions – Quanitifiers• This will match abc, abd, abe and any combination of ab followed by a digit.

* The character can appear zero or more times

+ The character can appear one or more times

? The character can appear zero or one times

{n,m} The character can appear at least n times, and no more than m.

Either parameter can be omitted to indicated a minimum limit

with nomaximum, or a maximum limit without aminimum,

but not both.

ab?c matches both ac and abc,

ab{1,3}c matches abc, abbc and abbbc.

Example

Page 28: Intoduction to php  strings

Regular Expressions – Sub-Expressions

• A sub-expression is a regular expression contained within the

main regular expression (or another sub-expression); you define

one by encapsulating it in parentheses:

/a(bc.)e/

/a(bc.)+e/

Example

Page 29: Intoduction to php  strings

Matching and Extracting Strings

• The preg_match() function can be used to match a regular

expression against a given string.

$name = "Davey Shafik";

// Simple match

$regex = "/[a-zA-Z\s]/";

if (preg_match($regex, $name)) {

// Valid Name

}

Example

Page 30: Intoduction to php  strings

Questions?

‚A good question deserve a good grade…‛

Page 31: Intoduction to php  strings

Self Check !!

Page 32: Intoduction to php  strings

If this presentation helped you, please visit our page facebook.com/baabtra and like it.

Thanks in advance.

www.baabtra.com | www.massbaab.com |www.baabte.com

Page 33: Intoduction to php  strings

Contact Us

Emarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

Start up VillageEranakulam,Kerala, India.

Email: [email protected]