Showing posts with label string manipulation. Show all posts
Showing posts with label string manipulation. Show all posts

Thursday, September 18, 2008

The addcslashes() function

This function returns a string with backslashes added before certain characters.

Syntax: addcslashes(string,characters);

characters – the characters before which a backslash is to be added.

Example inputs:

1.
$str1=" my program";
echo $str1;
echo "
";
echo addcslashes($str1,"r");
echo "
";
echo addcslashes($str1,"r,m");

2.
$str1=" my program";
echo $str1;
echo "
";
echo addcslashes($str1,"a...p");

In this case, a backslash is added before every character that falls between a and p in the alphabetical sequence.

3.
$str1=" 121232342434";
echo $str1;
echo "
";
echo addcslashes($str1,"1...3");

Example outputs:

1.
my program my p\rog\ram \my p\rog\ra\m

2.
my program \my \pr\o\gr\a\m

3.
121232342434 \1\2\1\2\3\2\34\24\34

Tuesday, September 16, 2008

The strtouper() function

This function returns a given string in upper case.

Syntax: strtoupper(string);

Example input:

$str1=" my PrOgRam";
echo $str1;
echo "
";
echo strtoupper($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,"@");

Sunday, September 14, 2008

The chr() function

chr() returns the character corresponding to an ASCII value

Example inputs:

echo chr(100);
echo "
";
echo chr(101);
echo "
";
echo chr(0101);
echo "
";
echo chr(0XFF);

The strlen() function

The strlen() function returns the length of string.

Syntax: strlen(string);

Example inputs:

$str1= "A simple PHP program";
echo strlen($str1);

Saturday, September 13, 2008

The strrev() function

This function is used to reverse any given string.
Syntax: strrev(string);

Example inputs:

$str1= "A simple PHP program";
echo $str1."
";
$str2 = strrev($str1);
echo $str2."
";
echo $str1."
";
echo strrev($str1);

The chop() function

This has the same syntax and performs the same function rtrim() does.

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.

Concatenation

In PHP, strings could be easily concatenated using the dot (.) operator.

Example inputs

$name=”Rick”;
echo “My name is “.$name;
2.
echo “My name is “.”Rick”;
3.
$str1 = “My name is”;
$str2 = “Rick”;
$str3 = $str1.$str2;

Example outputs

1.
My name is Rick.
2.
My name is Rick
3.
My name is Rick