Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

Thursday, September 18, 2008

The wordwrap() function

This function wraps a string into a new line when it reaches a specified length.

Syntax: wordwrap(string[,length,break,cut]);

length – the number of characters after which the string is wrapped. The default length is 75.

break – the character to be used as a separator. “\n” is the default.

cut – If this is TRUE, words longer than the specified length will also be wrapped.

If this value is FALSE, words longer than the specified length will not be wrapped. FALSE is the default value.

Example inputs:

1.
$str1="This is a simple PHP program";
echo $str1;
echo "
";
$str2 = wordwrap($str1,2,"
",true);
echo $str2;

2.
$str1="This is a simple PHP program";
echo $str1;
echo "
";
$str2 = wordwrap($str1,2,"
");
echo $str2;

In the second example, we have not mentioned any value for the cut parameter. The default value is FALSE and hence, words in the string that are longer than the specified length(here 2) will not be wrapped.

Example outputs:

1.
This is a simple PHP program Th is is a si mp le PH P pr og ra m

2.
This is a simple PHP program This is a simple PHP program

Saturday, September 13, 2008

trim() function

The trim() function could be used to remove certain pre-defined characters from both ends of a string.
Syntax: trim(string, [list of characters]);
string- the string in which the predefined characters are to be removed.
list of characters.
\0 for NULL
\t for TAB
\n for new line
“ ” for whitespace
If the list of characters are not specified, all pre-defined characters would be removed.
Example inputs
1.
$first_name = “James ”;
echo $first_name.”
”;
trim($first_name);
echo first_name;
In the output, the second echo function prints $first_name that has one less space.

2.
$name = “John
”;
echo $name.”
”;
trim($name);
echo $name;
In this case, the second time $name is printed, the new line character is absent.

Wednesday, September 3, 2008

A PHP File

A PHP program could be written with a simple text editor such as notepad. In order to develop web pages that look better, web development applications such as Macromedia Dreamweaver could also be used.


A PHP file would contain PHP statements, functions etc. as well as HTML tags and scripts. However, there could be PHP files that contain no HTML.
PHP files are usually saved with the extension, ".php”.
All statements in PHP end with a semicolon.