Operations with strings


<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
Using PHP string functions
Author: Elena Machkasova elenam@morris.umn.edu
Last modified: 3/13/08
-->
<html  xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>
Using PHP string functions
</title>
</head>
<body>
<h2>Some things you can do with strings</h2>
<?php
$str1 = "apples";
$str2 = "bananas and oranges";
print "<h3>Finding the length of a string</h3>\n";
print "<p>\n";
print "String \"$str1\" has ".strlen($str1)." characters<br/>\n";
print "String \"$str2\" has ".strlen($str2)." characters<br/>\n";
print "</p>\n";

print "<h3>Finding which sring is first in \"alphabetical\" order</h3>\n";

print "<p>\n";
if (strcmp($str1, $str2) < 0) {
	print "\"$str1\" is before \"$str2\" in alphabetical order<br/>\n";	
} else {
	print "\"$str2\" is before \"$str1\" in alphabetical order<br/>\n";		
}

$str3 = "Kiwi";
if (strcmp($str1, $str3) < 0) {
	print "\"$str1\" is before \"$str3\" in alphabetical order<br/>\n";	
} else {
	print "\"$str3\" is before \"$str1\" in alphabetical order<br/>\n";		
}
print "</p>\n";

print "<h3>Finding if strings are equal</h3>\n";
print "<p>\n";
$str3 = "apples";
if (strcmp($str1,$str3) == 0) {
	print "\"$str1\" and \"$str3\" are the same - Duh!<br/>\n";	
}
$str3 = "Apples";
if (strcmp($str1,$str3) == 0) {
	print "\"$str1\" and \"$str3\" are the same<br/>\n";	
} else {
	print "But \"$str1\" and \"$str3\" are different<br/>\n";
}
print "Comparing with strcasecmp:<br/>\n";
if (strcasecmp($str1,$str3) == 0) {
	print "\"$str1\" and \"$str3\" are the same<br/>\n";	
} else {
	print "But \"$str1\" and \"$str3\" are different<br/>\n";
}
print "</p>\n";

print "<h3>More stuff you can do with strings</h3>\n";

print "<p>\n";
$str4 = strtoupper($str1);
print "$str4 is the uppercase version of $str1<br/>\n";
$str5 = ucfirst($str2);
$str6 = ucwords($str2);
print "Capitalized the first letter in string: $str5<br/>\n";
print "Capitalized the first letter in all words: $str6<br/>\n";
print "The string itself didn't change: $str2<br/>\n";
print "</p>\n";
?>
</body>
</html>
http://rynite.morris.umn.edu/~elenam/1101_spring08/strings/strings.php

UMM CSci 1101