Monthly archive for April 2010

How can we Calculate the similarity between two strings

April 11, 2010 | Filed Under PHP | Comments Off

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

?>

Article written by admin

Return ASCII value of character in php?

April 11, 2010 | Filed Under PHP | Comments Off

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";
}
?>
Article written by admin

How can we format a number as “22.00″ in php ?

April 11, 2010 | Filed Under PHP | Comments Off

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

?>

Article written by admin

how can we insert HTML line breaks before all newlines in a string

April 11, 2010 | Filed Under PHP | Comments Off

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
Article written by admin

what does money_format() function in php?

April 11, 2010 | Filed Under PHP | Comments Off

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)

?>

Article written by admin

what is difference between explode and implode functions?

April 11, 2010 | Filed Under PHP | Comments Off

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
?>
Article written by admin

Count character in the string “Two Ts and one F.” in php ?

April 11, 2010 | Filed Under PHP | Comments Off

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

Return Values

Depending on mode count_chars() returns one of the following:

  • 0  – an array with the byte-value as key and the frequency of every byte as value.
  • 1 – same as 0  but only byte-values with a frequency greater than zero are listed.
  • 2 – same as 0 but only byte-values with a frequency equal to zero are listed.
  • 3 – a string containing all used byte-values cheapest is returned.
  • 4 – a string containing all not used byte-values is returned.

<?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.


Article written by admin

what is difference between addslashes() and addcslashes()

April 11, 2010 | Filed Under PHP | Comments Off

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;">);
?>
addcslashes — Quote string with slashes in a C style

Returns a string with backslashes before characters that are listed in charlist parameter.

<?php
echo addcslashes("zoo['.']", 'z..A');
// output:  \zoo['\.']
?>

Article written by admin

what is addcslashes() in php?

April 11, 2010 | Filed Under PHP | Comments Off

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['\.']
?>
Article written by admin

What’s the difference between htmlentities() and htmlspecialchars()?

April 11, 2010 | Filed Under PHP | Comments Off

htmlentities — Convert all applicable characters to HTML entities

<?php
$str
= "A 'quote' is <b>bold</b>";
// Outputs: A 'quote' is &lt;b&gt;bold&lt;/b&gt;
echo htmlentities($str);
// Outputs: A &#039;quote&#039; is &lt;b&gt;bold&lt;/b&gt;
echo htmlentities style="color: #007700;">($str, ENT_QUOTES);
?>
htmlspecialchars much  should i take — Convert special characters to HTML entities
<?php
$new
= htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo
$new; // &lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;
?>

Article written by admin

© PHPInterviewQuestion.com 2009 - 2012

eXTReMe Tracker