Sunday, October 26, 2008

The convert_cyr_string() function

This function converts a string in one Cyrillic character set to another.

syntax: convert_cyr_string(string,from,to);

from – the character set from which the string should be converted.
to – the character set to which the string should be converted.

The from and two parameters can hold the following any of the following values.

k - koi8-r
w - windows-1251
i - iso8859-5
a - x-cp866
d - x-cp866
m - x-mac-cyrillic

Example input:

$str = "This is Vince æøå";
echo $str;
echo "
echo convert_cyr_string($str,'w','a');

Example output:

This is Vince æøå
This is Vince ¦è¥

Friday, October 17, 2008

The str_split() function

This function splits a string into an array of strings.

syntax: str_split(string[,length]);

string – the input string
length – the number of characters each element in the output array must hold. The default value is 1.

Example input:

$str1="This is a simple program number";

Example output:

Array ( [0] => This [1] => is a [2] => simpl [3] => e pro [4] => gram [5] => numbe [6] => r )

Thursday, October 16, 2008

The str_rot13() function

This function performs ROT13 encoding on a string.

Syntax: str_rot13(string);

Example input:

$str1="This is a simple program number 1";
$str2= str_rot13($str1);
echo $str2;
echo "
echo $str3= str_rot13($str2);

Example output:

Guvf vf n fvzcyr cebtenz ahzore 1
This is a simple program number 1

What is ROT13?

A ROT13 enoding algorithm moves every character in a string 13 places forward. Numbers and special characters, however, are untouched.

Another interesting fact about the ROT13 algorithm is that the same algorithm could be used for encoding as well as decoding as you’d understand when you analyze the example.

myLot User Profile

Friday, October 10, 2008

The str_pad() function

This string could be used to pad a string to a new length.

Syntax: str_pad(string,length[,pad-string,pad-type]);

string – the input string

length – the length of the padded string. If this value is lesser than length of the actual string, no effect will take place

pad-string – the string to be used for padding. If this value is not specified, white spaces will be added as padding.

pad-type –
This parameter can have the following values:

STR_PAD_RIGHT: This is the default value and adds padding to the right of the string.
STR_PAD_LEFT: This value adds padding to the left end of the string.
STR_PAD_BOTH : This value adds padding to both the ends. If the number of characters to be added is not even, the right side is added an extra character.

Example input:

$str1="This is a simple program";
echo str_pad($str1,40,"*",STR_PAD_BOTH);

Example output:

********This is a simple program********

Wednesday, October 1, 2008

The str_shuffle() function

This function randomly shuffles the characters in the string.

Syntax: str_shuffle(string);

string – the input string

Example input:

echo str_shuffle("Hello");
echo "
echo str_shuffle("Hello");

Example output:


The ord() function

The ord() function returns the ASCII value of the first character of the string.

Syntax: ord(string);

string – the input string

Example input:

echo ord("Hello");

Example output:


The str_repeat() function

This function is used to repeat printing a string a certain number of times.

Syntax: str_repeat(string,count);

string – the input string
count – the number of times the input string needs to be printed.
Example input:
echo str_repeat("*",10);
Example output:
PS: This function could be used to create interesting patterns.