Showing posts with label working with strings. Show all posts
Showing posts with label working with strings. Show all posts

Tuesday, September 16, 2008

The strtolower() function

This function returns the lowercase version of a given string.

Syntax: strtolower(string);

Example input:

$str1=" MY PROGRAM";
echo strtolower($str1);
echo "
";
echo $str1;

Example output:

my program

MY PROGRAM

Monday, September 15, 2008

The chunk_split() function

This function split a string into smaller strings.

Syntax: chunk_split(string,length,seperator);

length – the number of characters each smaller strings should contain.
seperator – this character will be inserted after every smaller string.
Example inputs:
$str1="A simple PHP Program";
echo chunk_split($str1,2,"@");

Saturday, September 13, 2008

The ltrim() function

ltrim() removes whitespace and certain pre-defined characters from the left end of the string.
Example inputs:
$str1=" A simple PHP Program";
echo $str1;
echo "
";
echo ltrim($str1);

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.