<html>
<body bgcolor=lightyellow>
<H1 Align=center>String Functions</h1>
<?php
$string = "the quick brown fox jumped over the lazy dog";
// length of string --------------------------------------------
print "<b>The string is......: </b>" . $string;
print "<br>";
print "<b>The length of the string is: </b>" . strlen($string);
print "<hr>";
// Change case -------------------------------------------------
print "<b>the string in uppercase is: </b>" . strtoupper($string);
print "<br>";
print "<b>The string capitalized is..: </b>" . ucfirst($string);
print "<br>";
print "<b>Each word capitalized is..: </b>" . ucwords($string);
print "<hr>";
// Finding substrings ------------------------------------------
print "<b>Where is 'fox'.......? </b>" . strpos($string, 'fox');
print "<br>";
print "<b>Where is 'the'.......? </b>" . strpos($string, 'the');
print "<br>";
print "<b>Where is 2nd 'the'? </b>" . strpos($string, 'the', 10);
print "<br>";
print "<b>Where is 'hound'..? </b>";
$found = strpos($string, 'hound');
var_dump($found);
print "<hr>";
// Extracting substrings ---------------------------------------
print "<b>Extract the 'dog'..: </b>" . substr($string, 41, 3);
print "<br>";
print "<b>'fox' to the end.....: </b>" .
substr($string, strpos($string, 'fox') );
print "<br>";
print "<b>Beginning to 'fox': </b>" .
substr($string, 0, strpos($string, 'fox'));
print "<hr>";
// Replacing substrings ---------------------------------------
print "<b>Switch the 'fox' with the 'dog': </b><br>";
$newString = $string;
$newString = str_replace('fox', 'xxx', $newString);
$newString = str_replace('dog', 'fox', $newString);
$newString = str_replace('xxx', 'dog', $newString);
print "<b>Now the string is: </b>" . $newString;
print "<hr>";
// Scrambling a string -----------------------------------------
print "<b>Generate a stronger password </b><br>";
$myPswd = 'mysecretpassword';
print "<b>My old password is..: </b>" . $myPswd;
print "<br>";
$alpha = 'abcdefghijklmnopqrstuvwxyz ';
$scramble = 'kjYhzde51Eyk0F8nx6fn875rh60';
$newString = strtr($myPswd, $alpha, $scramble);
print "<b>Now the password is: </b>" . $newString;
print "<hr>";
// Converting a string to an array ------------------------------
$array = explode(" ", $string);
// $array = preg_split("/\s/", $string); //can use regex
print "<b>Converting string to an array: </b><br>";
print_r($array);
print "<hr>";
// Converting an array to a string ------------------------------
$newString = implode("_", $array);
print "<b>Converting an array back to a string: </b><br>";
print $newString;
print "<hr>";
// Converting a keyed array to a string -------------------------
$array2 = array('father'=>'Sam', 'Mother'=>'Carol', 'brother'=>'john'); //a keyed array
$newString = implode("_", $array2); //only array values
print "<b>Converting a keyed array to a string: </b><br>"; //will be used
print $newString;
print "<hr>";
// Formatting a number into a string ----------------------------
$formatted = sprintf("Formatted: %5.2f and $%6.3f", 10/4, 2651.6666); //2.50 $2651.667
print "<b>10 divided by 4 is 2.5 using %5.2f, and 2651.6666 using $%6.3f is printed as: </b> $formatted";
print "<hr>";
// Adding backslashes to a string --------------------------------
$fname = 'Sam';
$lname = 'Sultan';
$str = "insert into table
values('$fname','$lname',10)";
print "<b>the string before </b> $str";
print "<br>";
$str = addslashes($str);
print "<b>the string after </b> $str";
print "<hr>";
// Validating an email address ----------------------------------
$email = "sam.sultan@nyu.e";
$pattern = "/^[A-z][A-z0-9_]+(\.[A-z0-9_]+)*@[A-z0-9_]+(\.[A-z0-9_]+)*\.[A-z]{2,4}$/";
$ok = preg_match($pattern, $email);
print "<b>Is sam.sultan@nyu.e a valid email? </b> $ok <br>";
print "<hr>";
// Eliminating all html tags from a string -----------------------
$string1 = "<u>I am entering some data with <b>HTML</b> tags </u>";
echo "<b>The string with html tags is.............: </b>" . $string1 . "<br>";
$string2 = preg_replace("/<.+?>/", "", $string1);
echo "<b>The string without any html tags is: </b>" . $string2 . "<br>";
print "<hr>";
?>
<?php include "../include.php"; ?> <!-- hyperlink to see the code -->
</body>
</html>