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

Monday, April 20, 2009

The levenshtein() function

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.

Friday, April 17, 2009

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
******

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, September 19, 2008

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

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

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

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 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);