Showing posts with label php examples. Show all posts
Showing posts with label php examples. Show all posts

Monday, April 27, 2009

Some PHP Exercises

Write a PHP program to set your locale information to that of theUnited Kingdom and display the currency of the UK. Once you are done, set the locale information to default.

Friday, April 24, 2009

Some PHP Exercises

What is the use of the setlocale() function?

Write a program to set your locale information to the UK and then revert back to your default locale information.

Friday, April 17, 2009

Some PHP Exercises

  • Write a program using the count_chars() function and determine the ASCII values of all English alphabets.

  • How would you find the list of all characters that are not present in a string?

Monday, September 29, 2008

The strcmp() function

The strcmp() function compares two strings and returns an integer accordingly.

Syntax: strmp(string1,string2);

This function returns 0 if both the strings are equal.
It returns a value less than zero, if string1 is less than string2.
It returns an integer greater than zero if string1 is greater than string2.

Example input:

$str1 = "Hello";
$str2 = "Halo";
echo strcmp($str1,$str2);

Example output:

1

Tuesday, September 23, 2008

The substr_replace() function

This function could be used to replace all or a portion of a string with another.

Syntax: substr_replace(string,sub-string,start[,length]);

sub-string – the string that replaces all or portion of the larger string.

start – the position in the original string from where the replacement should start.

If the start parameter is positive, the position is calculated from the beginning of the string.
If the start parameter is negative, the position is calculated from the end of the string.
A start parameter with the value zero would mean that the replacement should start from the first character of the string.

length – the number of characters that should be replaced.

A positive value for the length parameter indicates the number of characters to be replaced.
A negative value for the length parameter indicates the number of characters to be left at the end of the string.
Example input:
$str1="His name is James";
echo $str1;
echo "
";
echo $str2 = substr_replace($str1,"Her",0,3);
echo "
";
echo $str3 = substr_replace($str2,"Marie",-5);
echo "
";
echo substr_replace($str3," ",0,-5);
Example output:
His name is James
Her name is James
Her name is Marie
Marie

The substr_count() function

This function could be used to count the number of times a sub-string occurs within a given string.

Syntax: substr_count(string,sub-string[,start,length]);

sub-string- the string to be searched for
start – that position in the larger string from where the search should begin
length – length of the search from the starting position

If start and length parameters are not specified, substr_count() carries out search in the entire string.
Example input:
$str1="This is a simple PHP program";
echo $str1;
echo "
";
echo substr_count($str1,"PHP",3,20);
echo "
";
echo substr_count($str1,"PHP",3,10);
echo "
";
echo substr_count($str1,"i");
Example output:
This is a simple PHP program 1 0 3

The strtok() function

This function could be used to split a string into smaller strings.

Syntax: strtok(string,split);

split – the split character
Example input:
$str1="This is a simple PHP program";
echo $str1;
echo "
";
echo $str2 = strtok($str1," ");
echo "
";
echo strtok(" ");
echo "
";
echo strtok(" ");
Example output:
This is a simple PHP program This is a
A good look at the example and you will realize that the string is specified only for the first time strtok() is called. For subsequent calls, the string need not be mentioned as the function keeps track of the current string.

Friday, September 19, 2008

The substr() function

This function could be used to extract a certain part of the string.

Syntax: substr(string,start[,length]);

start – the starting position from where a sub-string should be extracted.
length – the number of characters the extracted string should contain.

Both start and length can also contain negative values.

A negative start parameter indicates that the position should be determined from the end of the string.

A negative length parameter would extract the certain number of characters from the end of the string.

If the length parameter is not specified, the rest of the string from the specified position would be returned.

Example inputs:

1.
$str1="This is a simple PHP program";
echo $str1;
echo "
";
echo substr($str1,17,3);
Here,the function returns the three characters after position 17 in $str1.

2.
$str1="This is a simple PHP program";
echo $str1;
echo "
";
echo substr($str1,-17);

3.
$str1="This is a simple PHP program";
echo $str1;
echo "
";
echo substr($str1,-17,-3);

Example outputs:

1.
This is a simple PHP program PHP
2.
This is a simple PHP program imple PHP program
3.
This is a simple PHP program imple PHP prog

The strstr() function

This function is used to identify the position of a string within a larger string.
If our search string is not found, this function returns FALSE.

Syntax: strstr(string,search string);

search string – the sub-string that is to be searched in the larger string.

Example input:

$str1="This is a simple PHP program";
echo $str1;
echo "
";
echo strstr($str1,"PHP");
echo "
";
echo strstr($str1,"p");

Example output:

This is a simple PHP program PHP program ple PHP program

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