Featured Posts

How to break a string into pieces in PHP

Posted by admin | Posted in PHP, Software Programs | Posted on 24-11-2009

0

You need to break a string into pieces. For example, you want to access each line that a user enters in a <textarea> form field.
Use explode( ) if what separates the pieces is a constant string:

<?php
$words = explode(‘ ‘,’My sentence is not very complicated’);
?>

Use split( ) or preg_split( ) if you need a POSIX or Perl-compatible regular expression to describe the separator:

<?php
$words = split(‘ +’,'This sentence  has  some extra whitespace  in it.’);
$words = preg_split(‘/\d\. /’,'my day: 1. get up 2. get dressed 3. eat toast’);
$lines = preg_split(‘/[\n\r]+/’,$_REQUEST['textarea']);
?>

Use spliti( ) or the /i flag to preg_split( ) for case-insensitive separator matching:

<?php
$words = spliti(‘ x ‘,’31 inches x 22 inches X 9 inches’);
$words = preg_split(‘/ x /i’,’31 inches x 22 inches X 9 inches’);
?>

The simplest solution of the bunch is explode( ). Pass it your separator string, the string to be separated, and an optional limit on how many elements should be returned:

<?php
$dwarves = ‘dopey,sleepy,happy,grumpy,sneezy,bashful,doc’;
$dwarf_array = explode(‘,’,$dwarves);
?>

This makes $dwarf_array a seven-element array, so print_r($dwarf_array) prints:
Array
(
[0] => dopey
[1] => sleepy
[2] => happy
[3] => grumpy
[4] => sneezy
[5] => bashful
[6] => doc
)

If the specified limit is less than the number of possible chunks, the last chunk contains the remainder:

<?php
$dwarf_array = explode(‘,’,$dwarves,5);
print_r($dwarf_array);
?>

This prints:
Array
(
[0] => dopey
[1] => sleepy
[2] => happy
[3] => grumpy
[4] => sneezy,bashful,doc
)

The separator is treated literally by explode( ). If you specify a comma and a space as a separator, it breaks the string only on a comma followed by a space, not on a comma or a space.
With split( ), you have more flexibility. Instead of a string literal as a separator, it uses a POSIX regular expression:

<?php
$more_dwarves = ‘cheeky,fatso, wonder boy, chunky,growly, groggy, winky’;
$more_dwarf_array = split(‘, ?’,$more_dwarves);
?>

This regular expression splits on a comma followed by an optional space, which treats all the new dwarves properly. A dwarf with a space in his name isn’t broken up, but everyone is broken apart whether they are separated by “,” or “, “. print_r($more_dwarf_array) prints:
Array
(
[0] => cheeky
[1] => fatso
[2] => wonder boy
[3] => chunky
[4] => growly
[5] => groggy
[6] => winky
)

Similar to split( ) is preg_split( ), which uses a Perl-compatible regular expression engine instead of a POSIX regular expression engine. With preg_split( ), you can take advantage of various Perl-ish regular expression extensions, as well as tricks such as including the separator text in the returned array of strings:

<?php
$math = “3 + 2 / 7 – 9″;
$stack = preg_split(‘/ *([+\-\/*]) */’,$math,-1,PREG_SPLIT_DELIM_CAPTURE);
print_r($stack);
?>

This prints:
Array
(
[0] => 3
[1] => +
[2] => 2
[3] => /
[4] => 7
[5] => -
[6] => 9
)

The separator regular expression looks for the four mathematical operators (+, -, /, *), surrounded by optional leading or trailing spaces. The PREG_SPLIT_DELIM_CAPTURE flag tells preg_split( ) to include the matches as part of the separator regular expression in parentheses in the returned array of strings. Only the mathematical operator character class is in parentheses, so the returned array doesn’t have any spaces in it.

VN:F [1.9.3_1094]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

How to Trim Blanks from a String using PHP

Posted by admin | Posted in PHP, Software Programs, Theory Subjects | Posted on 23-11-2009

0

Use ltrim( ), rtrim( ), or trim( ), ltrim( ) removes whitespace from the beginning of a string, rtrim( ) from the end of a string, and trim( ) from both the beginning and end of a string:

<?php
$zipcode = trim($_REQUEST['zipcode']);
$no_linefeed = rtrim($_REQUEST['text']);
$name = ltrim($_REQUEST['name']);
?>
For these functions, whitespace is defined as the following
characters:newline, carriage return, space, horizontal and
vertical tab, and null .

Trimming whitespace off of strings saves storage space and canmake for more
precise display of formatted data or text within<pre> tags, for example. If
you are doing comparisons with userinput, you should trim the data first, so
that someone who mistakenly enters”98052″ as their zip code isn’t forced to
fix an error that really isn’t one. Trimming before exact text comparisons
also ensures that, for example, “salami\n” equals “salami.” It’s also a good
idea to normalize string data bytrimming it before storing it in a database.

The trim( ) functions can also remove user-specifiedcharacters from strings.
Pass the characters you want to remove as a secondargument. You can indicate
a range of characters with two dots between the firstand last characters in
the range:

<?php
// Remove numerals and space from the beginning of the line
print ltrim('10 PRINT A$',' 0..9');
// Remove semicolon from the end of the line
print rtrim('SELECT * FROM turtles;',';');
?>

This prints:

PRINT A$
SELECT * FROM turtles

PHP also provides chop( ) asan alias for rtrim( ). However, you’re best off
using rtrim( ) instead because PHP’s chop( ) behaves differently than Perl’s
chop( ) (which is deprecated in favor of chomp( ) , anyway), and using it can
confuse others when they read your code.

VN:F [1.9.3_1094]
Rating: 6.0/10 (1 vote cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Interpolating Functions and Expressions Within Strings Using PHP

Posted by Dora_david | Posted in HTML, Software Programs, Theory Subjects | Posted on 23-11-2009

0

You can put variables, object properties, and array elements (if the subscript is unquoted) directly in double-quoted strings:

<?php
print "I have $children children.";
print "You owe $amounts[payment] immediately.";
print "My circle's diameter is $circle->diameter inches.";
?>

Interpolation with double-quoted strings places some limitations on the syntax of what can be interpolated. In the previous example, $amounts['payment'] had to be written as $amounts[payment] so it would be interpolated properly. Use curly braces around more complicated expressions to interpolate them into a string. For example:

<?php
print "I have less than {$children} children.";
print "You owe {$amounts['payment']} immediately.";
print "My circle's diameter is {$circle->getDiameter()} inches.";
?>

Direct interpolation or using string concatenation also works with heredocs. Interpolating with string concatenation in heredocs can look a little strange because the closing heredoc delimiter and the string concatenation operator have to be on separate lines:

<?php
print <<< END
Right now, the time is
END
. strftime('%c') . <<< END
 but tomorrow it will be
END
. strftime('%c',time() + 86400);
?>

Also, if you’re interpolating with heredocs, make sure to include appropriate spacing for the whole string to appear properly. In the previous example, Right now the time has to include a trailing space, and but tomorrow it will be has to include leading and trailing spaces.

VN:F [1.9.3_1094]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

How to capitalize, lowercase letters in a string using PHP

Posted by admin | Posted in PHP, Software Programs, Theory Subjects | Posted on 20-11-2009

0

Use ucfirst( ) to capitalize the first character in a string:

<?php
print ucfirst(‘monkey face’);
print ucfirst(’1 monkey face’);
?>

Output:

Monkey face
1 monkey face

Note that the second phrase is not “1 Monkey face.”

Use ucwords( ) to capitalize the first character of each word in a string:

<?php
print ucwords(’1 monkey face’);
print ucwords(“don’t play zone defense against the philadelphia 76-ers”);
?>

Output:

1 Monkey Face
Don’t Play Zone Defense Against The Philadelphia 76-ers

As expected, ucwords( ) doesn’t capitalize the “t” in “don’t.” But it also doesn’t capitalize the “e” in “76-ers.” For ucwords( ), a word is any sequence of nonwhitespace characters that follows one or more whitespace characters. Since both ‘ and – aren’t whitespace characters, ucwords( ) doesn’t consider the “t” in “don’t” or the “e” in “76-ers” to be word-starting characters.
Both ucfirst( ) and ucwords( ) don’t change the case of non-first letters:

<?php
print ucfirst(‘macWorld says I should get an iBook’);
print ucwords(‘eTunaFish.com might buy itunaFish.Com!’);
?>

Output:
MacWorld says I should get an iBook
ETunaFish.com Might Buy ItunaFish.Com!

The functions strtolower( ) and strtoupper( ) work on entire strings, not just individual characters. All alphabetic characters are changed to lowercase by strtolower( ) and strtoupper( ) changes all alphabetic characters to uppercase:

<?php
print strtolower(“I programmed the WOPR and the TRS-80.”);
print strtoupper(‘”since feeling is first” is a poem by e. e. cummings.’);
?>

Output:
i programmed the wopr and the trs-80.
“SINCE FEELING IS FIRST” IS A POEM BY E. E. CUMMINGS.

VN:F [1.9.3_1094]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

How to Switching tabs and spaces in PHP

Posted by admin | Posted in PHP, Software Programs, Theory Subjects | Posted on 20-11-2009

0

Use str_replace( ) to switch spaces to tabs or tabs to spaces
Example

<?php
$r = mysql_query(“SELECT message FROM messages WHERE id = 1″) or die();
$ob = mysql_fetch_object($r);
$tabbed = str_replace(‘ ‘,”\t”,$ob->message);
$spaced = str_replace(“\t”,’ ‘,$ob->message);
print “With Tabs: <pre>$tabbed</pre>”;
print “With Spaces: <pre>$spaced</pre>”;
?>

Using str_replace( ) for conversion, however, doesn’t respect tab stops. If
you want tab stops every eight characters, a line beginning with a five-letter
word and a tab should have that tab replaced with three spaces, not one.
Use the pc_tab_expand( ) function
pc_tab_expand( )

<?php
function pc_tab_expand($text) {
while (strstr($text,”\t”)) {
$text = preg_replace_callback(‘/^([^\t\n]*)(\t+)/m’,'pc_tab_expand_helper’, $text);
}
return $text;
}
function pc_tab_expand_helper($matches) {
$tab_stop = 8;
return $matches[1] .
str_repeat(‘ ‘,strlen($matches[2]) *
$tab_stop – (strlen($matches[1]) % $tab_stop));
}
$spaced = pc_tab_expand($ob->message);
?>

You can use the pc_tab_unexpand( ) function to turn spaces back to tabs
pc_tab_unexpand( )

<?php
function pc_tab_unexpand($text) {
$tab_stop = 8;
$lines = explode(“\n”,$text);
foreach ($lines as $i => $line) {
// Expand any tabs to spaces
$line = pc_tab_expand($line);
$chunks = str_split($line, $tab_stop);
$chunkCount = count($chunks);
// Scan all but the last chunk
for ($j = 0; $j < $chunkCount – 1; $j++) {
$chunks[$j] = preg_replace(‘/ {2,}$/’,”\t”,$chunks[$j]);
}
// If the last chunk is a tab-stop’s worth of spaces
// convert it to a tab; Otherwise, leave it alone
if ($chunks[$chunkCount-1] == str_repeat(‘ ‘, $tab_stop)) {
$chunks[$chunkCount-1] = “\t”;
}
// Recombine the chunks
$lines[$i] = implode(”,$chunks);
}
// Recombine the lines
return implode(“\n”,$lines);
}
$tabbed = pc_tab_unexpand($ob->message);
?>

Both functions take a string as an argument and return the string appropriately modified.
Each function assumes tab stops are every eight spaces, but that can be modified by changing the setting of the $tab_stop variable.
The regular expression in pc_tab_expand( ) matches both a group of tabs and all the text in a line before that group of tabs. It needs to match the text before the tabs because the length of that text affects how many spaces the tabs should be replaced with so that subsequent text is aligned with the next tab stop. The function doesn’t just replace each tab with eight spaces; it adjusts text after tabs to line up with tab stops.
Similarly, pc_tab_unexpand( ) doesn’t just look for eight consecutive spaces and then replace them with one tab character. It divides up each line into eight-character chunks and then substitutes ending whitespace in those chunks (at least two spaces) with tabs. This not only preserves text alignment with tab stops; it also saves space in the string.

VN:F [1.9.3_1094]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Tags and Elements of HTML

Posted by admin | Posted in Theory Subjects, XML | Posted on 17-11-2009

0

If you look at the first and last lines of the code for the last example, you will see pairs of angle brackets containing the letters <html>. The two brackets and all of the characters between them are known as a tag, and there are lots of tags in the example. All of the tags in this example come in pairs; there are
opening tags and closing tags. The closing tag is always slightly different than the opening tag in that it has a forward slash character before the characters </html>.

html

html

The special meaning these tags give is a description of the structure of the document. The opening tag says “This is the beginning of a heading” and the closing tag says “This is the end of a heading.”Without the markup, the words in the middle would just be another bit of text; it would not be clear that they formed the heading.
Now look at the paragraph of text about the company; it is held between an opening <p> tag and a closing </p> tag. And, you guessed it, the p stands for paragraph.

As you can see, the markup in this example actually describes what you will find between the tags, and the added meaning the tags give is describing the structure of the document. For example, between the opening <p> and closing </p> tags are paragraphs and between the <h1> and </h1> tags is a heading. Indeed, the whole HTML document is contained between opening <html> and closing </html> tags.
If you were wondering why there is a number 1 after the h , it is because in HTML and XHTML there are six levels of headings. A level 1 heading is sometimes used as the main heading for a document (such as a chapter title), which can then contain subheadings, with level 6 being the smallest. This allows you to structure your document appropriately with subheadings under the main heading.

VN:F [1.9.3_1094]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Sample HTML program discussion

Posted by admin | Posted in HTML, Software Programs, Theory Subjects | Posted on 17-11-2009

0

HTML, or Hypertext Markup Language, is the most widely used language onWeb. As its name suggests, HTML is a markup language, which may sound complicated, although really you come across markup every day. Markup is just something you add to a document to give it special meaning; for example, when you use a highlighter pen you are marking up a document. When you are marking up a document for the Web, the special meaning you are adding indicates the structure of the document, and the markup indicates which part of the document is a heading, which parts are paragraphs, what belongs in a table, and so on. This markup in turn allows a Web browser to display your document appropriately.

When creating a document in a word processor, you can distinguish headings using a heading style (usually with a larger font) to indicate which part of the text is a heading. You can use the Enter (or Return) key to start a new paragraph. You can insert tables into your document, create bulleted lists,
and so on. When marking documents up for the Web you are performing a very similar process. HTML and XHTML are the languages you use to tell aWeb browser where the heading is for aWeb page, what is a paragraph, what is part of a table and so on, so it can structure your document and render it properly. But what is the difference between HTML and XHTML?Well, first you should know that there are several versions of both HTML and XHTML, but don’t let that bother you—it all sounds a lot more complicated than it really is. Whereas there are several versions of HTML, each version just adds functionality on top of its predecessor (like a new version of some software might add some features or a new version of a dictionary might add a few extra words), or offers better ways of doing things that were already in earlier versions. So, you do not need to learn each version of HTML and XHTML, nor do you need to focus on one variation. This book teaches you all you need to know to writeWeb pages using HTML and XHTML. Indeed, as I mentioned in the Introduction, XHTML is just like the latest version of HTML, as you will see shortly (although to be accurate, while it is almost identical to the last version of HTML, it is technically HTML’s successor).

Sample Code :

<html>
<head>
<title>Vdiscussion: About Us</title>
</head>
<body>
<h1>About Vdiscussion.com</h1>
<p>Vdiscussion is free educational site
</p>
</body>
</html>

Save this code as filename.html.

VN:F [1.9.3_1094]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Style sheet linking in XML

Posted by ma.vinothkumar | Posted in Theory Subjects, XML | Posted on 12-11-2009

0

This technique, you link a style sheet to the XML document. A style sheet is a separate file that contains instructions for formatting the individual XML elements. You can use either a cascading style sheet (CSS)—which is also used for HTML pages—or an Extensible Stylesheet Language Transformations (XSLT) style sheet—which is considerably more powerful than a CSS and is designed specifically for XML documents.

VN:F [1.9.3_1094]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Does XML Replace HTML ?

Posted by ma.vinothkumar | Posted in Theory Subjects, XML | Posted on 12-11-2009

0

Currently, the answer to that question is no. HTML is still the primary language
used to tell browsers how to display information on the Web.
With Internet Explorer, the only practical way to dispense entirely with HTML
when you display XML is to attach a cascading style sheet to the XML docu-
ment and then open the document directly in the browser. However, using a cas-
cading style sheet is a relatively restrictive method for displaying and working
with XML. All the other methods you’ll learn in this book involve HTML. Data
binding and XML DOM scripts both use HTML Web pages as vehicles for dis-
playing XML documents. And with XSLT style sheets, you create templates that
transform the XML document into HTML that tells the browser how to format
and display the XML data.
Rather than replacing HTML, XML is currently used in conjunction with
HTML and vastly extends the capability of Web pages to:
I Deliver virtually any type of document
I Sort, filter, rearrange, find, and manipulate the information in
other ways
I Present highly structured information
As the quotation at the beginning of the chapter states, XML was designed for
interoperability with HTML.

VN:F [1.9.3_1094]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

What is XML ?

Posted by ma.vinothkumar | Posted in Theory Subjects, Uncategorized, XML | Posted on 12-11-2009

0

XML stands for Extensible Markup Language, was defined by the
XML Working Group of the World Wide Web Consortium (W3C). This group
described the language as follows:
The Extensible Markup Language (XML) is a subset of SGML…Its
goal is to enable generic SGML to be served, received, and processed
on the Web in the way that is now possible with HTML. XML has been
designed for ease of implementation and for interoperability with both
SGML and HTML.

As you can see, XML is a markup language designed specifically for delivering
information over the World Wide Web, just like HTML (Hypertext Markup
Language), which has been the standard language used to create Web pages
since the inception of the Web. Since we already have HTML, which continues
to evolve to meet additional needs, you might wonder why we require a com-
pletely new language for the Web.

VN:F [1.9.3_1094]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)