Strings ======= See also https://www.w3schools.com/php/php_ref_string.asp NOTE: There is a significant difference between " and ' ---- $user = "Bill"; echo 'Hi $user'; //-> Hi $user echo "Hi $user"; //-> Hi Bill Length of String: ---------------- strlen($s) Comparing Strings regardless of Capitalization: ----------------------------------------------- $a = "Bill"; $b = "bill"; strcasecmp($a, $b) returns 0 printf() (formatted text): -------- $price = 5; $tax = 0.075; printf('The dish costs $%.2f', $price * (1 + $tax)); //prints 'The dish costs $5.38 %.2f is the formatting string (see http://php.net/manual/en/function.sprintf.php for more details) .n: floating point number with n digits after the period padding character: space or 0 d: minimum width +,-: display sign in front of number $zip = '5432'; printf("ZIP is %05d); //prints '05432' $month = 2; $day = 6; $year = 2007; printf("The date is %02d/%02d/%d, $month, $day, $year); //prints 'The date is 02/06/2007 $min = -40; $max = 40; printf("Temperature will be between %+d and %+d degrees Celsius.", $min, $max); //prints "... -40 and +40 ..." array explode ( string $delimiter , string $string [, int $limit = PHP_INT_MAX ] ) ------------- NOTE: This is called 'split' in many other languages!!! $str = "Hello world. It's a beautiful day."; print_r (explode(" ",$str)); int strpos ( string $haystack , mixed $needle [, int $offset = 0 ] ) ---------- Returns first position $t is found in $s, otherwise returns false $s = "test string"; $target = "str"; $iPos = strpos($s, $target); if ($iPos === false) { print "There was no '" . $target . "' in the string '" . $s . "'"; } else { print "Target '" . $target . "' found at position " . $iPos . " in '" . $s . "'"; } strstr( string $haystack , mixed $needle [, bool $before_needle = FALSE ] ) : string ------------------------------------------------------------------------------------ Returns part of haystack string starting from and including the first occurrence of needle to the end of haystack. $s = "test string"; $strFound = strstr($s, 'str'); //returns 'string' $strNotFound = strstr($s, 'x'); //returns false str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] ) : mixed ------------ print str_replace('{class}',$my_class, 'Fried Bean Curd Oil-Soaked Fish'); This prints: Fried Bean Curd Oil-Soaked Fish $oldtxt = "Hello World!"; $newtxt = ("World","Dolly" ,$oldtxt ) //"Hello Dolly!" string substr ( string $string , int $start [, int $length ] ) ------------- print substr('watch out for that tree',6,5); //returns 'out f' print substr('watch out for that tree',-3); //returns 'ree' print substr('watch out for that tree',15, -2); //returns 'hat tr' mixed substr_replace ( mixed $string , mixed $replacement , mixed $start [, mixed $length ] ) -------------------- applies to both a string and an array of strings Without the $length argument, substr_replace() replaces everything from $start to the end of the string. If $length is specified, only that many characters are replaced. If $start is negative, the new substring is placed by counting $start characters from the end of $old_string, not from the beginning. If $start and $length are 0, the new substring is inserted at the start of $old_string. print substr_replace('My pet is a blue dog.','fish.',-9); //prints 'My pet is a fish. print substr_replace('My pet is a blue dog.','green',-9,4); //prints 'My pet is a green dog.' Example for an array of strings: $input = array('A: XXX', 'B: XXX', 'C: XXX'); // A simple case: replace XXX in each string with YYY. echo implode('; ', substr_replace($input, 'YYY', 3, 3))."\n"; // A more complicated case where each replacement is different. $replace = array('AAA', 'BBB', 'CCC'); echo implode('; ', substr_replace($input, $replace, 3, 3))."\n"; // Replace a different number of characters each time. $length = array(1, 2, 3); echo implode('; ', substr_replace($input, $replace, 3, $length))."\n"; Result: A: YYY; B: YYY; C: YYY A: AAA; B: BBB; C: CCC A: AAAXX; B: BBBX; C: CCC string wordwrap ( string $str [, int $width = 75 [, string $break = "\n" [, bool $cut = FALSE ]]] ) --------------- $s = "Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty and dedicated to the proposition that all men are created equal."; print "
\n".wordwrap($s, 50)."\n
"; bool is_numeric ( mixed $var ) --------------- 'here document' (see 'Learning PHP 5': -------------- $str = << Menu

Dinner

  • Beef Chow-Fun
  • Sauteed Pea Shoots
  • Soy Sauce Noodles
BLOCK; CONVERSIONS ========== Converting characters in string to ASCII: ---------------------------------------- $nArray = str_split($uName); foreach ($nArray as $char) { $c = ord($char); echo "{$c} + "; } Converting ascii to character: ----------------------------- $char = char($ascii); Converting String to Numnber ---------------------------- $num = "3.14"; $int = (int)$num; $float = (float)$num; $double = (double)$num; Converting to Upper Case: ------------------------ $str = "test"; $strUpperCase = strtoupper($str); //returns 'TEST' Avoiding to need to use escape quotes with heredoc syntax --------------------------------------------------------- Example 1: $fish = 'whiting'; $book['title'] = 'Alice in Wonderland'; $mockTurtle = <<< Gryphon "Will you walk a little faster?" said a $fish to a snail. "There's a porpoise close behind us, and he's treading on my tail." (from {$book['title']}) Gryphon; echo $mockTurtle; Example 2: $strText = <<Back HTML; print $strText