Function printf() like C style printf

Function printf() is like C style printf. An example:
This is a number: 535
First number: 25
Second number: 36
Code in PHP:
printf("This is a number: %d", 535 );
printf("First number: %d<br> Second number: %d<br> ", 25, 36 );

Example of different formating:

Decimal: 543
Binary: 1000011111
Double: 543.000000
Octal: 1037
String: 543
Hex (lower): 21f
Hex (upper): 21F
Code in PHP:
printf("Decimal: %d<br>", $number);
printf("Binary: %b<br>", $number);
printf("Double: %f<br>", $number);
printf("Octal: %o<br>", $number);
printf("String: %s<br>", $number);
printf("Hex (lower): %x<br>", $number);
printf("Hex (upper): %X<br>", $number);

Specifying a field width
               Books
                 CDs
               Games
           Magazines
Left aligned        
Books               
CDs                 
Games               
Magazines           
Code in PHP:
<pre>
printf("%20s ", "Books");
printf("%20s ", "CDs");
printf("%20s ", "Games");
printf("%20s ", "Magazines");
printf("%- 20s ", "Left aligned");
printf("%-20s ", "Books");
printf("%-20s ", "CDs");
printf("%-20s ", "Games");
printf("%-20s ", "Magazines");
</pre>
Using printf() to format a list product prices
Name                                  Price
                                           
Green armchair                    222.40
Candlestick                         4.00
Coffee table                       80.60
Code in PHP:
$products = Array( "Green armchair"=>"222.4","Candlestick"=>"4","Coffee table"=>80.6);
print "<pre>";
printf("%- 20s%23s ", "Name", "Price");
printf("%'- 43s ", "");
foreach ( $products as $key=>$val )
printf( "%- 20s%20.2f ", $key, $val );
printf("</pre>");

Using sprintf() function in connection with PHP print()
You have 2.33 dollars to spend

String as an array
$test = scallywag
$test[0] = s
$test[2] = a
strlen($test) = 9
Code in PHP:
$dosh = sprintf("%.2f", 2.334454);
print "<br>You have $dosh dollars to spend";
print"<br>";
print "<br>String as an array<br>";
$test = "scallywag";
print "$test = $test<br>";
print "$test[0] = $test[0]<br>";
print "$test[2] = $test[2]<br>"
;$len = strlen($test);
print "strlen($test) = $len<br>";

Finding a substring with strstr(), stristr()



Finding a substring with strstr(), stristr() is not case sensitive
$membership = pAB7
Findng AB: strstr( $membership, "AB" )
Thank you. Don't forget that your membership expires soon!

Finding the Position of a Substring with strpos()
$membershop = mz00xyz
Finding position of "mz": strpos($membership, "mz")
hello mz
Code in PHP:
$membership = "pAB7";
print "$membership = $membership<br>";print "Findng AB: strstr( $membership, "AB" )<br>";
if( strstr( $membership, "AB" ) )
print "Thank you. Don't forget that your membership expires soon!";
else
print "Thank you!";
print"<br><br>Finding the Position of a Substring with strpos()<br>";
$membership = "mz00xyz";
print "$membershop = $membership<br>";
print "Finding position of "mz": strpos($membership, "mz")";
if ( strpos($membership, "mz") === 0 )
print "<br>hello mz";

Extracting Part of a String with substr()

Extracting Part of a String with substr()
$test = scallywag
substr($test,6) = wag
substr($test,6,2) = wa
$test = ququqa@quyubbte.co.uk
Extracting last 3 characters: substr( $test, - 3 )
Don't forget our special offers for British customers
Code in PHP:
$test = "scallywag";
print "$test = $test<br>";
$s = substr($test,6);
print "substr($test,6) = $s<br>";
$s = substr($test,6,2);
print "substr($test,6,2) = $s<br>";
$test = "ququqa@quyubbte.co.uk";
print "$test = $test<br>";
print "Extracting last 3 characters: substr( $test, - 3 )<br>";
if ( $test = substr( $test, - 3 ) == ".uk" )
print "Don't forget our special offers for British customers";
else
print "Welcome to our shop!";

Dividing a String into Tokens with strtok()

Dividing a String into Tokens with strtok()
http://www.degreggja.com/qs.xp
OP=dnquery.xp
ST=MS
DBS=2
QRY=developer+php

Code in PHP:
$test = "http://www.degreggja.com/qs.xp?OP=dnquery.xp&ST=MS&DBS=2&QRY=developer+php";
$delims = "?&";
$word = strtok( $test, $delims );
while ( is_string( $word ) )
{
if ( $word ) print "$word<br>";
$word = strtok( $delims);
}

Cleaning Up a String with trim() and ltrim()

Cleaning Up a String with trim() and ltrim()
text = 			lots of room to breath 
lots of room to breath

Code in PHP:
print "<pre>";
$text = " lots of room to breath ";
print "text = $text<br>";
$text = trim( $text );
print $text;
print "</pre>";

Replacing a Portion of a String using substr_replace()

Replacing a Portion of a String using substr_replace()
$membership = mz99xyz
New membership number: mz00xyz

Replacing Substrings Using str_replace
$string = Site contents copyright 1999. The 1999 Guide to All Things Good in the world
Site contents copyright 2000. The 2000 Guide to All Things Good in the world

Converting Case
$membership = mz00xyz after strtoupper():
MZ00XYZ

$home_url = WWW.GFIOEPRTGJOIPJGLFDGJK.CO.UK after strtolower():
http://www.gfioeprtgjoipjglfdgjk.co.uk
Code in PHP:
MZ00XYZ = "mz99xyz";
print "$membership = $membership<br>";
$membership = substr_replace( $membership, "00", 2, 2 );
print "New membership number: $membership&lp;p>";
print "Replacing Substrings Using str_replace<br>";
$string = "Site contents copyright 1999. ";
$string .= "The 1999 Guide to All Things Good in the world";
print "$string = $string<br>";
print str_replace("1999","2000",$string);
print "<br><br>Converting Case<br>";
$membership = "mz00xyz";
print "$membership = $membership after strtoupper():<br>";
$membership = strtoupper( $membership );
print "$membership<P>";
$home_url = "WWW.GFIOEPRTGJOIPJGLFDGJK.CO.UK";
print "$home_url = $home_url after strtolower():<br>";
$home_url = strtolower( $home_url );
if ( ! ( strpos ( $home_url, "http://" ) === 0 ) )
$home_url = "http://http://www.gfioeprtgjoipjglfdgjk.co.uk";
print $home_url;

ucwords function:

ucwords function:
$full_name = violet elizabeth deft, after ucwords():
Violet Elizabeth Deft

combination of ucwords and strtolower:
$full_name = VIolEt eLIZaBeTH dEFt, after functions strtolower and ucwords:
Violet Elizabeth Deft
Breaking string with explode() function
$start_date = 2000-01-12
$date_array = explode("-", $start_date)
2000
01
12



Back to main page