Using similar_text() get similarity between two strings.
Return Values
Returns the number of matching chars in both strings.
example
<?php
$first =’php3′;
$first =’php4′;
echo retail price similar_text ( $first, $second ) //3
?>
Using similar_text() get similarity between two strings.
Returns the number of matching chars in both strings.
example
<?php
$first =’php3′;
$first =’php4′;
echo retail price similar_text ( $first, $second ) //3
?>
using ord() method we can get ASCII value of character in php.
<?php
$str = "\n" style="color: #007700;">;
if (ord style="color: #0000bb;">$str) == 10) {
echo "The first character of \$str is a line feed.\n";
}
?> using number_format() function.
<?php
$number = 1234.56;
// english notation (default)
$english_format_number = number_format( style=”color: #0000bb;”>$number);
// 1,235
// French notation
$nombre_format_francais style=”color: #007700;”>= number_format($number, 2, style=”color: #dd0000;”>’,’, ‘ ’);
// 1 234,56
$number = 1234.5678;
// english notation without thousands seperator
$english_format_number = number_formatviagra order style=”color: #007700;”>($number, 2, ‘.’, ”);
// 1234.57
?>
using nl2br function
nl2br — Inserts HTML line breaks before all newlines in a string
n style="color: #000000;"><?php
style="color: #007700;">echo nl2br("foo isn't\n bar");
?>
The above example will output:
foo isn't<br /> bar
money_format — Formats a number as a currency string
We will use different locales and format specifications to illustrate the use of this function.
<?php
$number = 1234.56;
// let’s print the international format for the en_US locale
style=”color: #0000bb;”>setlocale(LC_MONETARY, ‘en_US’);
echo money_format(‘%i’, $number) . “\n”;
// USD 1,234.56
// Italian national format with 2 decimals`
setlocale(LC_MONETARY, ‘it_IT’);
echo money_format(‘%.2n’, $number) . “\n”;
// L. 1.234,56
// Using a negative number
$number = -1234.5672;
// US national format, using () for negative numbers
// and 10 digits for left precision
setlocale(LC_MONETARY, ‘en_US’);
echo money_format(‘%(#10n’, $number) . “\n”;
// ($ 1,234.57)
// Similar format as above, adding the use of 2 digits of right
// precision and ’*' as a fill character
echo money_format(‘%=*(#10.2n’, $number) . “\n”;
// ($********1,234.57)
// Let’s justify to the left, with 14 positions of width, 8 digits of
// left precision, 2 of right precision, withouth grouping character
// and using the international format for the de_DE locale.
setlocale(LC_MONETARY, ‘de_DE’);
echo money_format(‘%=*^-14#8.2i’, 1234.56) . “\n”;
// DEM 1234,56****
// Let’s add some blurb before and after the conversion specification
style=”color:#0000bb;”>setlocale(LC_MONETARY, ‘en_GB’);
$fmt = ‘The final value is %i (after a 10%% discount)’;
echo money_format($fmt, 1234.56) . “\n”;
// The final value is GBP 1,234.56 (after a 10% discount)
?>
explode — Split a string by string
explode, split a string and return an array by using niddle in the string.
Example . explode()
<?php
// Example 1
$history= "php4 php5 php6";
$pieces = (" ", $history);
echo $history[0]; // php3
echo $history[1]; // php4
echo $history[1]; // php5
?>
implode — Join array elements with a string
Example: implode() example
<?php
$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);
echo $comma_separated; // lastname,email,phone
?> To count the total number of characters in the given string.
we will use count_chars() funtion
count_chars — Return information about characters used in a string
Depending on mode count_chars() returns one of the following:
<?php
$data = "Two Ts and one F.";
foreach (count_chars($data, 1) as $i => $val) {
echo “There were $val instance(s) of \”" , chr($i) , “\” in the string.\n”;
}
?> The above example will output:
There were 4 instance(s) of " " in the string. There were 1 instance(s) of "." in the string. There were 1 instance(s) of "F" in the string. There were 2 instance(s) of "T" in the string. There were 1 instance(s) of "a" in the string. There were 1 instance(s) of "d" in the string. There were 1 instance(s) of "e" in the string. There were 2 instance(s) of "n" in the string. There were 2 instance(s) of "o" in the string. There were 1 instance(s) of "s" in the string. There were 1 instance(s) of "w" in the string.
addslashes — Quote string with slashes
Returns a string with backslashes before characters that need to be quoted in database queries etc. These characters are single quote (‘), double quote (“), backslash (\) and NUL (the NULL byte).
style="color: #000000;"><?php
$str = "Is your name O'reilly?";
// Outputs: Is your name O\'reilly? echo addslashes($str style="color: #007700;">);
?>Returns a string with backslashes before characters that are listed in charlist parameter.
<?php
echo addcslashes("zoo['.']", 'z..A');
// output: \zoo['\.']
?>
addcslashes — Quote string with slashes in a C style
style="color: #0000bb;"><?php
addcslashes('foo[ ]', 'A..z');
// output: \f\o\o\[ \]
// All upper and lower-case letters will be escaped
// ... but so will the [\]^_` and any tabs, line
// feeds, carriage returns, etc.
style="color: #0000bb;">?><?php
echo addcslashes("zoo['.']", 'z..A');
// output: \zoo['\.']
?> htmlentities — Convert all applicable characters to HTML entities
<?php
$str = "A 'quote' is <b>bold</b>";
// Outputs: A 'quote' is <b>bold</b>
echo htmlentities($str);
// Outputs: A 'quote' is <b>bold</b>
echo htmlentities style="color: #007700;">($str, ENT_QUOTES);
?><?php
$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new; // <a href='test'>Test</a>
?> © PHPInterviewQuestion.com 2009 - 2012