This function calculates the Levenshtein distance between two strings.
syntax: levenshtein(string1,string2[,insert,replace,delete]);
string1 and string2 are the two input strings.
insert- the cost of inserting a character
replace - the cost of replacing a character
delete - the cost of deleting a character
By default, the cost for each operation is 1. However, we could also mention a cost, in which case, the Levenshtein cost would be calculated accordingly.
Example input:
$str1 = "John";
$str2 = "COrney";
echo levenshtein($str1,$str2);
echo "
";
echo levenshtein($str1,$str2,1,2,3);
Example output:
5
8
What is Levenshtein distance?
The Levenshtein distance between two strings is the minimum number of operations required to convert one string to another.
The operations might include insert, replace or a delete.
Showing posts with label PHP String functions. Show all posts
Showing posts with label PHP String functions. Show all posts
Monday, April 20, 2009
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?
The count_chars() function
This function can be used to determine the characters in a string and the number of times they are used.
syntax: count_chars(string,mode);
string – the input string
mode
This parameter will determine the output of the count_chars() function.
Following are the possible values and implications of this parameter.
0 – The function returns an array with the ASCII value of every possible character as the key and the number of occurrances in the string as the value.
1 – The function returns anarray with the ASCII values as keys and the number of occurrances as values. In this case, the function creates an array element for characters that occur atleast once in the string.
2 - The function returns anarray with the ASCII values as keys and the number of occurrances as values. The array elements will correspond to characters that are not present in the string.
3 – The function will just display all the characters that are present in the input string.
4 – The function displays all the characters that are not present in the input string.
Example input:
$str = "Rooney";
echo $str;
echo "
";
print_r(count_chars($str,1));
echo "
";
echo count_chars($str,3);
echo "
";
echo count_chars($str,4);
Example output:
Rooney
Array ( [82] => 1 [101] => 1 [110] => 1 [111] => 2 [121] => 1 )
Renoy
******
syntax: count_chars(string,mode);
string – the input string
mode
This parameter will determine the output of the count_chars() function.
Following are the possible values and implications of this parameter.
0 – The function returns an array with the ASCII value of every possible character as the key and the number of occurrances in the string as the value.
1 – The function returns anarray with the ASCII values as keys and the number of occurrances as values. In this case, the function creates an array element for characters that occur atleast once in the string.
2 - The function returns anarray with the ASCII values as keys and the number of occurrances as values. The array elements will correspond to characters that are not present in the string.
3 – The function will just display all the characters that are present in the input string.
4 – The function displays all the characters that are not present in the input string.
Example input:
$str = "Rooney";
echo $str;
echo "
";
print_r(count_chars($str,1));
echo "
";
echo count_chars($str,3);
echo "
";
echo count_chars($str,4);
Example output:
Rooney
Array ( [82] => 1 [101] => 1 [110] => 1 [111] => 2 [121] => 1 )
Renoy
******
Wednesday, January 7, 2009
UUEncoding and UUDecoding
convert_uuencode() function
This function encodes a string using the Unix-to-Unix encoding algorith.
syntax: convert_uuencode(string);
string – the input string
convert_uudecode() function
This function decodes a UU Encoded string.
snytax: convert_uudecode(string);
string – the encoded string
Example input:
$str = "This is Vince";
echo $str;
echo "";
echo convert_uuencode($str);
echo "";
echo convert_uudecode($encstr);
echo $str;
echo "";
echo convert_uuencode($str);
echo "";
echo convert_uudecode($encstr);
Example output:
This is Vince -5&AI
r!6:6yc90``>Read http://email.about.com/od/emailbehindthescenes/a/uuencoding.htm for more on UU encoding
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
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.
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";
print_r(str_split($str1,5));
Example output:
Array ( [0] => This [1] => is a [2] => simpl [3] => e pro [4] => gram [5] => numbe [6] => r )
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);
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:
lHole
oHell
Syntax: str_shuffle(string);
string – the input string
Example input:
echo str_shuffle("Hello");
echo "
";
echo str_shuffle("Hello");
Example output:
lHole
oHell
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:
72
Syntax: ord(string);
string – the input string
Example input:
echo ord("Hello");
Example output:
72
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.
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
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
The implode() function
The implode function is used to glue together elements of an array into a string.
Syntax: implode([separator, ]array);
separator - this parameter specifies the character or characters to be put between the array elements when they are glued together into a string. The default value is an empty space.
array – the input array
Example input:
$employee = array('This', 'is','John.');
echo implode(" ",$employee);
Example output:
This is John.
Syntax: implode([separator, ]array);
separator - this parameter specifies the character or characters to be put between the array elements when they are glued together into a string. The default value is an empty space.
array – the input array
Example input:
$employee = array('This', 'is','John.');
echo implode(" ",$employee);
Example output:
This is John.
Subscribe to:
Posts (Atom)