Presentation is loading. Please wait.

Presentation is loading. Please wait.

String functions http://www.w3schools.com/php/php_ref_string.asp.

Similar presentations


Presentation on theme: "String functions http://www.w3schools.com/php/php_ref_string.asp."— Presentation transcript:

1 String functions

2 String Length strlen() Returns the length of a string str_word_count()
Count the number of words in a string

3 echo and print echo() Outputs strings print() Outputs a string
printf() Outputs a formatted string sprintf() Writes a formatted string to a variable fprintf() Writes a formatted string to a specified output stream vprintf() vsprintf() vfprintf() number_format() Formats a number with grouped thousands

4 printf( ) <?php $num1= ; echo "$num1<br/>"; printf("%f<br/>", $num1); printf("%.2f", $num1); // printf("%f<br/>%.2f", $num1, $num1); ?>

5 number_format() Price: NT$45,080 Number: 315 Total: NT$14,200,200.00
<?php $price= 45080; $num = 315; $tot = $price * $num; echo "Price: NT\$", number_format($price),"<br/>"; echo "Number: $num<br/>"; echo "Total: NT\$", number_format($tot, 2),"<br/>"; ?> Price: NT$45,080 Number: 315 Total: NT$14,200,200.00

6 Split & Join explode() Breaks a string into an array str_split()
Splits a string into an array implode() Returns a string from the elements of an array join() Alias of implode()

7 explode( ) <?php $strW = "HTML, CSS, JavaScript, PHP"; $arrW = explode(", ",$strW); echo "<ul>"; for ($i=0;$i<count($arrW);$i++) echo "<li>$arrW[$i]</li>"; echo "</ul>"; ?>

8 implode( ) <?php $arrW = array("HTML", "CSS", "JavaScript", "PHP"); echo "[" . implode("] [",$arrW) . "]"; ?>

9 ltrim( ), rtrim( ), trim( )
Strips whitespace from both sides of a string chop() Alias of rtrim() rtrim() Strips whitespace from the right side of a string ltrim() Strips whitespace from the left side of a string <?php $hw = " Hello World! "; echo "\"$hw\"<br/>"; echo "ltrim: \"" . ltrim($hw) . "\"<br/>"; echo "rtrim: \"" . rtrim($hw) . "\"<br/>"; echo "trim: \"" . trim($hw) . "\"<br/>"; ?>

10 Replace, Convert str_replace()
Replaces some characters in a string (case-sensitive) str_ireplace() Replaces some characters in a string (case-insensitive) substr_replace() Replaces a part of a string with another string str_pad() Pads a string to a new length str_repeat() Repeats a string a specified number of times str_shuffle() Randomly shuffles all characters in a string strrev() Reverses a string strtolower() Converts a string to lowercase letters strtoupper() Converts a string to uppercase letters ucfirst() Converts the first character of a string to uppercase ucwords() Converts the first character of each word in a string to uppercase addslashes() Returns a string with backslashes in front of predefined characters stripslashes() Unquotes a string quoted with addslashes()

11 Substring, Search strlen() Returns the length of a string substr()
Returns a part of a string strstr() Finds the first occurrence of a string inside another string (case-sensitive) strchr() Finds the first occurrence of a string inside another string (alias of strstr()) stristr() Finds the first occurrence of a string inside another string (case-insensitive) strrchr() Finds the last occurrence of a string inside another string strpos() Returns the position of the first occurrence of a string inside another string (case-sensitive) stripos() Returns the position of the first occurrence of a string inside another string (case-insensitive) strrpos() Finds the position of the last occurrence of a string inside another string (case-sensitive) strripos() Finds the position of the last occurrence of a string inside another string (case-insensitive) strcmp() Compares two strings (case-sensitive) strcasecmp() Compares two strings (case-insensitive)

12 strpos( ) <?php $email = $_GET['mail']; //$_POST['mail']
if ($pos = strpos($ , { $user = substr($ ,0,$pos); $server = substr($ , $pos+1); echo " $ <br/>"; echo "User name: $user<br/>"; echo "Server: $server<br/>"; } else echo "<h2>Wrong !</h2>"; ?>


Download ppt "String functions http://www.w3schools.com/php/php_ref_string.asp."

Similar presentations


Ads by Google