Using substr()

<?php
print substr(‘abcdef’, 1); // bcdef
print substr(‘abcdef’, 1, 3); // bcd
print substr(‘abcdef’, 0, 4); // abcd
print substr(‘abcdef’, 0, 8 ); // abcdef
print substr(‘abcdef’, -1, 1); // f

// Accessing single characters in a string
// can also be achived using “curly braces”
$string = ‘abcdef’;
print $string{0}; // a
print $string{3}; // d
print $string{strlen($string)-1}; // f

?>

Negative start

<?php
$test_var = substr(“abcdef”, -1); // returns “f”
$test_var = substr(“abcdef”, -2); // returns “ef”
$test_var = substr(“abcdef”, -3, 1); // returns “d”
?>

String Functions (Len, Left, Right, Mid)

<%
dim myvar
myvar = "1234567"

response.Write(myvar & " is the variable<br/>")
response.Write(len(myvar) & " characters long<br/>")
response.Write(left(myvar,3) & " are the <b>left</b> three characters<br/>")
response.Write(right(myvar,3) & " are the <b>right</b> three characters<br/>")
response.Write(mid(myvar,3,3) & " : Mid(MyString, StartPos, NumChars)<br/> %>

Output:

1234567 is the variable
7 characters long
123 are the left three characters
567 are the right three characters
345 : Mid(MyString, StartPos, NumChars)

ASP: Find the length of a string using len and lenB function

my_string="Welcome to MySite.com"
Response.Write ( my_string )
Response.Write "Length of this string = " & len(my_string)
Response.Write "Number of bytes Required for this string = " & lenB(my_string)

The output of this will also show the number of bytes required. Here in this case it will print this:

Welcome to MySite.com
Length of this string = 21
Number of bytes Required for this string = 42

PHP: Expressions and Operators

Arithmetic Operators:

+ Addition Add two values
– Subtraction Subtract the second value from the first
* Multiplication Multiply two values
/ Division Divide the first value by the second
% Modulus Divide the first value by the second and return only the remainder
(for example, 7 % 5 yields 2)

Comparison Operators:

= = Equal Checks for equal values
= = = Identical Checks for equal values and data types
! = Not Equal Checks for values not equal
! = = Not Identical Checks for values not equal or not the same data type
< Less than Checks for one value being less than the second
> Greater than Checks for one value being greater than the second
< = Less than or Equal to Checks for on value being less than or equal to the second
> = Greater than or Equal to Checks for on value being greater than or equal to the second

Logical Operators:

And Checks if two or more statements are true
&& Same as And
Or Checks if at least one of two statements is true
|| Same as Or
Xor Checks if only one of two statements is true
! Checks if a statement is not true

Increment and Decrement Operators:

++value Pre-Increment Adds 1 to the value before processing the expression which uses the value
–value Pre-Decrement Subtracts 1 from the value before processing the expression which uses the value
value++ Post-Increment Adds 1 to the value after processing the expression which uses the value
value– Post-Decrement Subtracts 1 from the value after processing the expression which uses the value

eregi_replace() vs. ereg_replace()

Code:

<?php

$s = “Coding PHP is fun.”;

print “ereg_replace(): ” .
ereg_replace(“CODING”, “Learning”, $s) . “<br>”;
print “eregi_replace(): ” .
eregi_replace(“CODING”, “Learning”, $s);

?>

Output:

ereg_replace(): Coding PHP is fun.
eregi_replace(): Learning PHP is fun.
Explanation:

eregi_replace() is case insensitive, while ereg_replace() is not.

HTML Special Characters

&ndash;&#8211;

en dash
&mdash;&#8212;

em dash
¡&iexcl;&#161;inverted exclamation
¿&iquest;&#191;inverted question mark
"&quot;&#34;quotation mark
&ldquo;&#8220;left double curly quote
&rdquo;&#8221;right double curly quote
' &#39;apostrophe (single quote)
&lsquo;&#8216;left single curly quote
&rsquo;&#8217;right single curly quote
«


»

&laquo;


&raquo;

&#171;


&#187;

guillemets
 &nbsp;&#160;non-breaking space
Symbols
&&amp;&#38;ampersand
¢&cent;&#162;cent
©&copy;&#169;copyright
÷&divide;&#247;divide
>&gt;&#62;greater than
<&lt;&#60;less than
µ&micro;&#181;micron
·&middot;&#183;middle dot
&para;&#182;pilcrow (paragraph sign)
±&plusmn;&#177;plus/minus
&euro;&#8364;Euro
£&pound;&#163;British Pound Sterling
®&reg;&#174;registered
§&sect;&#167;section
&trade;&#153;trademark
¥&yen;&#165;Japanese Yen
Diacritics
á


Á

&aacute;


&Aacute;

&#225;


&#193;

lower-case "a" with acute accent


upper-case "A" with acute accent

à


À

&agrave;


&Agrave;

&#224;


&#192;

lower-case "a" with grave accent


upper-case "A" with grave accent

â


Â

&acirc;


&Acirc;

&#226;


&#194;

lower-case "a" with circumflex


upper-case "A" with circumflex

å


Å

&aring;


&Aring;

&#229;


&#197;

lower-case "a" with ring


upper-case "A" with ring

ã


Ã

&atilde;


&Atilde;

&#227;


&#195;

lower-case "a" with tilde


upper-case "A" with tilde

ä


Ä

&auml;


&Auml;

&#228;


&#196;

lower-case "a" with diaeresis/umlaut


upper-case "A" with diaeresis/umlaut

æ


Æ

&aelig;


&AElig;

&#230;


&#198;

lower-case "ae" ligature


upper-case "AE" ligature

ç


Ç

&ccedil;


&Ccedil;

&#231;


&#199;

lower-case "c" with cedilla


upper-case "C" with cedilla

é


É

&eacute;


&Eacute;

&#233;


&#201;

lower-case "e" with acute accent


upper-case "E" with acute accent

è


È

&egrave;


&Egrave;

&#232;


&#200;

lower-case "e" with grave accent


upper-case "E" with grave accent

ê


Ê

&ecirc;


&Ecirc;

&#234;


&#202;

lower-case "e" with circumflex


upper-case "E" with circumflex

ë


Ë

&euml;


&Euml;

&#235;


&#203;

lower-case "e" with diaeresis/umlaut


upper-case "E" with diaeresis/umlaut

í


Í

&iacute;


&Iacute;

&#237;


&#205;

lower-case "i" with acute accent


upper-case "I" with acute accent

ì


Ì

&igrave;


&Igrave;

&#236;


&#204;

lower-case "i" with grave accent


upper-case "I" with grave accent

î


Î

&icirc;


&Icirc;

&#238;


&#206;

lower-case "i" with circumflex


upper-case "I" with circumflex

ï


Ï

&iuml;


&Iuml;

&#239;


&#207;

lower-case "i" with diaeresis/umlaut


upper-case "I" with diaeresis/umlaut

ñ


Ñ

&ntilde;


&Ntilde;

&#241;


&#209;

lower-case "n" with tilde


upper-case "N" with tilde

ó


Ó

&oacute;


&Oacute;

&#243;


&#211;

lower-case "o" with acute accent


upper-case "O" with acute accent

ò


Ò

&ograve;


&Ograve;

&#242;


&#210;

lower-case "o" with grave accent


upper-case "O" with grave accent

ô


Ô

&ocirc;


&Ocirc;

&#244;


&#212;

lower-case "o" with circumflex


upper-case "O" with circumflex

ø


Ø

&oslash;


&Oslash;

&#248;


&#216;

lower-case "o" with slash


upper-case "O" with slash

õ


Õ

&otilde;


&Otilde;

&#245;


&#213;

lower-case "o" with tilde


upper-case "O" with tilde

ö


Ö

&ouml;


&Ouml;

&#246;


&#214;

lower-case "o" with diaeresis/umlaut


upper-case "O" with diaeresis/umlaut

ß&szlig;&#223;ess-tsett
ú


Ú

&uacute;


&Uacute;

&#250;


&#218;

lower-case "u" with acute accent


upper-case "U" with acute accent

ù


Ù

&ugrave;


&Ugrave;

&#249;


&#217;

lower-case "u" with grave accent


upper-case "U" with grave accent

û


Û

&ucirc;


&Ucirc;

&#251;


&#219;

lower-case "u" with circumflex


upper-case "U" with circumflex

ü


Ü

&uuml;


&Uuml;

&#252;


&#220;

lower-case "u" with diaeresis/umlaut


upper-case "U" with diaeresis/umlaut

ÿ&yuml;&#255;lower-case "y" with diaeresis/umlaut
´


`

 &#180;


&#96;

acute accent with no letter


grave accent/reversed apostrophe with no letter

PHP basename() Function

The basename() function returns the filename from a path.

<?php
$path = “/mywebsite/home.php”;

//Show filename with file extension
echo basename($path) .”<br/>”;

//Show filename without file extension
echo basename($path,”.php”);
?>

The output of the code above will be:

home.php
home