String functions http://www.w3schools.com/php/php_ref_string.asp
String Length $len = strlen("Hello World"); //11 Returns the length of a string str_word_count() Count the number of words in a string $len = strlen("Hello World"); //11 $wcnt = str_word_count("Hello World"); //2
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
printf( ) <?php $num1= 3.141592654; echo "$num1<br/>"; printf("%f<br/>", $num1); printf("%.2f", $num1); // printf("%f<br/>%.2f", $num1, $num1); ?>
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
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()
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>"; ?>
implode( ) <?php $arrW = array("HTML", "CSS", "JavaScript", "PHP"); echo "[" . implode("] [",$arrW) . "]"; ?>
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/>"; ?>
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()
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)
strpos( ) <?php $email = $_GET['mail']; //$_POST['mail'] if ($pos = strpos($email, '@')) { $user = substr($email,0,$pos); $server = substr($email, $pos+1); echo "E-mail: $email<br/>"; echo "User name: $user<br/>"; echo "Server: $server<br/>"; } else echo "<h2>Wrong E-mail!</h2>"; ?> If found, strpos() returns an integer. Otherwise, strpos() returns false; http://php.net/manual/en/function.strpos.php