1. Substring
substr()
string substr (string source, int begin, int [length]);
echo (substr("Christopher", 1));
will print hristopher
echo (substr("Christopher", -2));
will print er
echo (substr("Christopher", -5, 3));
prints 'oph'
2. ASCII Codes
chr()
-receives an integer that represents an ASCII code, and returns the corresponding character.
echo (chr(34));
Prints a quotation mark “
echo ("\"");
Prints a quotation mark “
ord()
-is chr()'s complement. It receives a character and returns the corresponding ASCII code as an integer.
Example
if ($c != ord(9) && $c != ord(13)) {
// Only append $c if not tab or enter
$my_string .= $c;
}
3. Length of the String
strlen()
-returns the number of characters in a string.
echo (strlen ("Christopher"));
Prints 11
4. String Formatting
printf()
-prints the formatted string to the browser without returning any value
Example 1
$dollars = "20 dollars";
printf ("%d", $dollars);
// Prints: 20
Example 2
$dollars = 20;
printf ("%.2f", $dollars);
// prints: $20.00
sprintf()
-returns the formatted string without printing it to the browser, so that you can store it in a variable or database
string sprintf(string format, mixed [args]...);
Example
$x = sprintf ("%'?-40.40s%'.3d%s", $ch4_title, $ch4_pg, "<BR>\n");
echo $x;
5. Type Specifier
d - Decimal integer.
b - Binary integer.
o - Octal integer.
x - Hexadecimal integer (with lowercase letters).
X - Hexadecimal integer (with uppercase letters).
c - Character whose ASCII code is integer value of the argument.
f - Double (Floating-point number).
e - Double, using exponential notation.
s - String.
% - A literal percent sign. This does not require a matching argument.
String Formatting – An Example
// Set variables:
$ch3_title = "Bicycle Safety";
$ch3_pg = 83;
$ch4_title = "Repairs and Maintenance";
$ch4_pg = 115;
// Print the TOC
printf ("%'.-40.40s%'.3d%s", $ch3_title, $ch3_pg, "<BR>\n");
printf ("%'.-40.40s%'.3d%s", $ch4_title, $ch4_pg, "<BR>\n");
This code will print:
Bicycle Safety...........................83
Repairs and Maintenance.................115
6. Number Formatting
number_format()
-formatting of numbers
string number_format (float num, int precision, string dec_point, string thousands_sep);
$num = 123456789.1234567;
echo (number_format ($num));
This prints: 123,456,789
Additional Example 1
$$num = 123456789.1234567;
echo (number_format ($num, 4));
This prints:
123,456,789.1235
Additional Example 2
$$num = 123456789.1234567;
echo (number_format ($num, 7, chr(44), " ")); // Note: chr(44) == comma
This prints:
123 456 789,1234567
7. Swapping of Arguments
Example
<?php
$dates = array(
array( 'mon'=> 12, 'mday'=>25, 'year'=>2001 ),
array( 'mon'=> 5, 'mday'=>23, 'year'=>2000 ),
array( 'mon'=> 10, 'mday'=>29, 'year'=>2001 )
);
$format = include ("local_format.php");
foreach ($dates as $date) {
printf( "$format", $date['mon'], $date['mday'], $date['year'] );
}
?>
Local_format.php
<?php
//return "%02d/%02d/%d<br/>";
return "%02d/%1\$02d/%2\$d<br/>";
?>
8. Position of a Substring
strpos()
-tells you both whether a string exists within a larger string and where it is to be found
Example
$membership = "mz00xyz";
if ( strpos($membership, "mz") === 0 )
{
print "hello mz";
}
9. Substring Within a String
strstr()
-used to test whether a string exists embedded within another string
Example
$membership = "pAB7";
if ( strstr( $membership, "AB") )
{ print "Thank you. Don't forget that your membership expires soon!"; }
else { print "Thank you!"; }
10. Tokenizing a String
strtok()
-Parse a string word by word using
Example
<?php
$test = "http://p24.corrosive.co.uk/tk.php";
$test .= "?id=353&sec=44&user=harry&context=php";
$delims = "?&";
$word = strtok( $test, $delims );
while ( is_string( $word ) ) {
if ( $word )
{ print "$word<br/>"; }
$word = strtok( $delims );
}?>
Output
http://p24.corrosive.co.uk/tk.php
id=353
sec=44
user=harry
context=php
11. Trimming the unnecessary
trim()
-simply strips the whitespace characters (spaces, tabs,newlines) from the beginning and end of a string and returns the "trimmed" string
echo (trim(" sample string "));
// prints 'sample string'
ltrim()
-strip white space only from the beginning of a string
Example
$text = "\t\t\tlots of room to breathe ";
$text = ltrim( $text );
print "<pre>$text</pre>";
// prints "lots of room to breathe ";
rtrim()
-strip white space only from the beginning of a string
Example
$text = "\t\t\tlots of room to breathe ";
$text = rtrim( $text );
print $text;
// prints " lots of room to breathe";
strip_tags()
-remove tags from a block to present it without formatting echo (trim(" sample string "));
Example
$string = "<p>I <i>simply</i> will not have it,";
$string .= "<br/>said Mr Dean</p><b>The end</b>";
print strip_tags( $string, "<br/>" );
12. Replacing a Portion of a String
substr_replace()
-works similarly to substr() except it enables you to replace the portion of the string you extract
Example
<? $membership = "mz99xyz";
$membership = substr_replace( $membership, "00", 2, 2);
print "New membership number: $membership<br/>";
// prints "New membership number: mz00xyz" ?>
Replacing Substrings
str_replace()
-replaces all instances of a string within another string
Example
$string = "Site contents copyright 2003.";
$string .= "The 2003 Guide to All Things Good in Europe";
print str_replace("2003","2004",$string);
Example
<?php
$source = array( "The package which is at version 4.2 was released in 2000", "The year 2000 was an excellent period for PointyThing4.2" );
$search = array( "4.2", "2000" );
$replace = array ( "5.0", "2001" );
$source = str_replace( $search, $replace, $source );
foreach ( $source as $str ) print "$str<br>";
// prints:
//The package which is at version 5.0 was released in 2001
//The year 2001 was an excellent period for PointyThing5.0 ?>
13. Converting Case
strtolower()
-convert a string to lowercase characters, use the function
Example
$membership = "mz00xyz";
$membership = strtoupper( $membership );
print "$membership<P>";
// prints "MZ00XYZ"
strtoupper()
-get an uppercase version of a string, use the function
Example
$home_url = "WWW.CORROSIVE.CO.UK";
$home_url = strtolower( $home_url );
if ( ! ( strpos ( $home_url, "http://") === 0) )
$home_url = "http://$home_url";
print $home_url;
// prints "http://www.corrosive.co.uk"
ucwords()
-makes the first letter of every word in a string uppercase
Example
$full_name = "VIolEt eLIZaBeTH bOTt";
$full_name = ucwords( strtolower($full_name) );
print $full_name;
// prints "Violet Elizabeth Bott"
14. Wrapping Text
nl2br()
-is a convenient method that converts every newline into an HTML break
Example
$string = "one line\n";
$string .= "another line\n";
$string .= "a third for luck\n";
print nl2br( $string );
prints the following:
one line<br />
another line<br />
a third for luck<br />
15. wordwrap()
-wraps lines every 75 characters and uses \n as its line-break character
Example 1
$string = "Given a long line, wordwrap() is useful as a means of"; $string .= "breaking it into a column and thereby making it easier to read";
print wordwrap($string);
would output
Given a long line, wordwrap() is useful as a means of breaking it into a column and thereby making it easier to read
Example 2
$string = "Given a long line, wordwrap() is useful as a means of";
$string .= "breaking it into a column and thereby making it easier to read";
print wordwrap( $string, 24, "<br/>\n");
Output
Given a long line,
<br/> wordwrap() is useful as
<br/> a means of breaking it
<br/> into a column and
<br/> thereby making it easier
<br/> to read
Example 3
$string = "As usual you will find me at http://www.witteringonaboutit.com/";
$string .= "chat/eating_green_cheese/forum.php. Hope to see you there!";
print wordwrap( $string, 24, "<br/>\n", 1 );
Output
As usual you will find<br/>
me at<br/>
http://www.witteringonab<br/>
outit.com/chat/eating_gr<br/>
een_cheese/forum.php.<br/>
Hope to see you there!
16. Breaking Strings into Arrays
explode()
-breaks up a string into an array, which you can then store, sort, or examine as you want
Example
$start_date = "2000-01-12";
$date_array = explode ("-",$start_date);
// $date[0] == "2000"
// $date[1] == "01"
// $date[2] == "12"
17. Formatting Numbers As Text
number_format()
-accepts a number to be transformed
Example
print number_format(100000.56 );
// 100,001
print number_format (100000.56, 2 );
// 100,001.56
print number_format(100000.56, 4 );
// 100,001.5600
print number_format (100000.56, 2, "-", " ");
// 100 000-56
18. Formatting Currency
money_format()
-determines the symbol used for currency, the decimal point character, and other attributes that change from region to region
Example
setLocale (LC_ALL, 'en_US');
$cash_array = array( 235.31, 5, 2000000.45 );
foreach ( $cash_array as $cash )
{ print money_format ("%\n", $cash); }
// $235.31
//$5.00
//$2,000,000.45
No comments:
Post a Comment