Entries by admin

sprintf()

March 28, 2011 | Filed Under PHP Tutorial, String Functions | Comments Off

This function writes a formatted string to a variable.

Syntax: sprintf(format,arg1,arg2,arg3,)

Description:

The first argument “format” is Required. It specifies the string and how to format the variables in it.

Possible format values:

* %% – Returns a percent sign
* %b – Binary number
* %c – The character according to the ASCII value
* %d – Signed decimal number
* %e – Scientific notation (e.g. 1.2e+2)
* %u – Unsigned decimal number
* %f – Floating-point number (local settings aware)
* %F – Floating-point number (not local settings aware)
* %o – Octal number
* %s – String
* %x – Hexadecimal number (lowercase letters)
* %X – Hexadecimal number (uppercase letters)

Additional format values. These are placed between the % and the letter (example %.2f):

* + (Forces both + and – in front of numbers. By default, only negative numbers are marked)
* ‘ (Specifies what to use as padding. Default is space. Must be used together with the width specifier. Example: %’x20s (this uses “x” as padding)
* – (Left-justifies the variable value)
* [0-9] (Specifies the minimum width held of to the variable value)
* .[0-9] (Specifies the number of decimal digits or maximum string length)

The second argument “arg1″ is Required. The argument to be inserted at the first %-sign in the format string.
The third argument “arg2″ is Optional. The argument to be inserted at the second %-sign in the format string.
The fourth argument “arg3″ is Optional. The argument to be inserted at the third, fourth, etc. %-sign in the format string.

Example:
Code:

$number = 154;
$value=sprintf( " number= %u",$number);
echo $value;

Output:

number= 154

Article written by admin

printf()

March 28, 2011 | Filed Under PHP Tutorial, String Functions | Comments Off

This function Output a formatted string.

Syntax: printf(format,arg1,arg2,arg3,)

Description:

The first argument “format” is Required. It specifies the string and how to format the variables in it.

Possible format values:

* %% – Returns a percent sign
* %b – Binary number
* %c – The character according to the ASCII value
* %d – Signed decimal number
* %e – Scientific notation (e.g. 1.2e+2)
* %u – Unsigned decimal number
* %f – Floating-point number (local settings aware)
* %F – Floating-point number (not local settings aware)
* %o – Octal number
* %s – String
* %x – Hexadecimal number (lowercase letters)
* %X – Hexadecimal number (uppercase letters)

Additional format values. These are placed between the % and the letter (example %.2f):

* + (Forces both + and – in front of numbers. By default, only negative numbers are marked)
* ‘ (Specifies what to use as padding. Default is space. Must be used together with the width specifier.
Example: %’x20s (this uses “x” as padding)
* – (Left-justifies the variable value)
* [0-9] (Specifies the minimum width held of to the variable value)
* .[0-9] (Specifies the number of decimal digits or maximum string length)

The second argument “arg1″ is Required. The argument to be inserted at the first %-sign in the format string.
The third argument “arg2″ is Optional. The argument to be inserted at the second %-sign in the format string.
The third argument “arg3″ is Optional. The argument to be inserted at the third, fourth, etc. %-sign in the format string.

Example1:
Code:

$str = "World";
$number = 154;
printf("Hello%s. Day %u",$str,$number);

Output:

HelloWorld. Day 154

Example2:
Code:

$number = 154;
printf("%f",$number);

Output:
154.000000

Article written by admin

wordwrap()

March 28, 2011 | Filed Under PHP Tutorial, String Functions | Comments Off

This function Wraps a string to a given number of characters using a string break character.
It returns the string broken into lines on success, or FALSE on failure.

Syntax: wordwrap(string,width,break,cut)

Description:

The first argument “string” is Required. It specifies the string to break up into lines.
The second argument “width” is Optional. It specifies the maximum line width. Default is 75
The first argument “break” is Optional. It specifies the characters to use as break. Default is “\n”
The first argument “cut” is Optional. It specifies whether words longer than the specified width should be wrapped. Default is FALSE (no-wrap).

Example1:
Code:

$text1 = "open your eyes to an experience that redifines your lifestyle.";
$newtext1 = wordwrap($text1, 15, "\n");

echo $newtext1;

Output:

open your eyes
to an
experience that
redifines your
lifestyle.

If you select “View source” in the browser window, you will see the following Output:

open your eyes
to an
experience that
redifines your
lifestyle.

Example2:
Code:

$text2 = "Social impact Awarrrrrrrrrrrrrrrrrd.";
$newtext2 = wordwrap($text2, 6, "\n", 1);

echo "$newtext2\n";

Output:

Social impact Awarrr rrrrrr rrrrrr rrd.

If you select “View source” in the browser window, you will see the following Output:

Social
impact
Awarrr
rrrrrr
rrrrrr
rrd.

Article written by admin

strcasecmp()

March 28, 2011 | Filed Under PHP Tutorial, String Functions | Comments Off

This function compares two strings.

It Returns < 0 if string1 is less than string2
> 0 if string1 is greater than string2
0 if they are equal.

Syntax: strcasecmp ( string1, string2 )

Description:

The first argument “string1″ is Required. It specifies the first string to compare.
The second argument” string2″ is Required. It specifies the second string to compare.

Note: This function is binary safe and case-insensitive.

Example:
Code:

$var1 = "World";
$var2 = "world";
  if(strcasecmp($var1,$var2)==0)
  {
  echo "both strings are in   caseinsensitive";
 }

Output:

both strings are in caseinsensitive

Article written by admin

strtr()

March 28, 2011 | Filed Under PHP Tutorial, String Functions | Comments Off

This function translate certain characters.This function returns a copy of str, translating all occurrences of each character in from to the corresponding character in to.

Syntax: strtr ( string , from, to )

Syntax: strtr ( string , array )

Description:

The first argument “string” is Required. It specifies the string to translate
The second argument “from” is Required (unless array is used). It specifies what characters to change
The third argument “to” is Required (unless array is used). Specifies what characters to change into
The fourth argument “array” is Required (unless to and from is used). An array containing what to change from as key, and what to change to as value.

Note: If from and to are different length, both will be formatted to the length of the shortest.

Example1:
Code:

 $string="I play Hockey";
 echo strtr($string,"Hockey","Carrom");

Output:

I plam Carrom

Example2:
Code:

$arr = array("Nokia" => "Samsung", "Karbon" => "Lava");
echo strtr("Nokia Karbon",$arr);

Output:

Article written by admin

substr_compare()

March 28, 2011 | Filed Under PHP Tutorial, String Functions | Comments Off

The substr_compare() function compares two strings from a specified start position.

This function returns:

* 0 – if the two strings are equal
* <0 - if string1 (from startpos) is less than string2
* >0 – if string1 (from startpos) is greater than string2

If length is equal or greater than length of string1, this function returns FALSE.

Syntax: substr_compare(string1,string2,startpos,length,case)

Description:

The first argument “string1″ is Required. It specifies the first string to compare.
The second argument “string2″ is Required. It specifies the second string to compare.
The third argument “startpos” is Required. It specifies where to start comparing in string1.
The fourth argument “length” is Optional. It specifies how much of string1 to compare.
The fifth argument “case” is Optional. It specifies whether or not to perform a case-sensitive compare. Default is FALSE (case-sensitive).If case_insensitivity is TRUE, comparison is case insensitive.

Example:
Code:

echo substr_compare("abcde", "cd", 2, 2);
echo substr_compare("abcde", "bcg", 1, 2);
echo substr_compare("abcde", "BC", 1, 2, true);
echo substr_compare("abcde", "bc", 1, 3);
echo substr_compare("abcde",  "cd", 1, 2);
echo substr_compare("abcde", "abc", 5, 1);

output:

0
0
0
1
-1
Warning: substr_compare() [function.substr-compare]: The length cannot exceed initial string length

Article written by admin

number_format()

March 28, 2011 | Filed Under PHP Tutorial, String Functions | Comments Off

This function Format a number with grouped thousands.
This function accepts either one, two or four arguments (not three):
Syntax: number_format(number,decimals,decimalpoint,separator)

Description:

The first argument ” number” is Required . If only one < argument is given, number will be formatted without decimals, but with a comma (",") between every group of thousands.

If two argument are given, number will be formatted with decimals decimals with a dot (".") in front, and a comma (",") between every group of thousands.

The second argument "decimals" is Optional. It specifies how many decimals. If this argument is set, the number will be formatted with a dot (.) as decimal point.

The third argument "decimalpoint" is Optional. It specifies what string to use for decimal point.

The fourth argument "separator" is Optional. It specifies what string to use for thousands separator.
Only the first character of separator is used. For example, For example, "zzz" will give the same output as "z".

Example:
Code:


$number = 1543.67;

echo $format1 = number_format($number)."
"; echo $format2 = number_format($number, 2, ',', ' ')."
"; $number = 1543.6776; echo $format3 = number_format($number, 2, '.', '');

Output:

1,544
1543,67
1543.68

Article written by admin

stripos()

March 28, 2011 | Filed Under PHP Tutorial, String Functions | Comments Off

This function returns the position of the first occurrence of a string inside another string.
If the string is not found, this function returns FALSE.
Syntax:stripos(string,find,start)

Description:

The first argument “string” is Required. It specifies the string to search.
The second argument “find” is Required. It specifies the string to find.
The third argument “start” is Optional. It specifies where to begin the search.

Note: The stripos() function is case-insensitive.

Example:
Code:

";
}

if ($position2 != false) {
    echo "We found '$search' in '$string2' at position $position2";
}

Output:

The string ‘a’ was not found in the string ‘pqrs’
We found ‘a’ in ‘SAD’ at position 1

Article written by admin

stristr()

March 28, 2011 | Filed Under PHP Tutorial, String Functions | Comments Off

This function searches for the first occurrence of a string inside another string.
This function returns the rest of the string (from the matching point), or FALSE, if the string to search for is not found.

Syntax:stristr(string,search)

Description:

The first argument “string” is Required. It specifies the string to search
The second argument “search” is Required. It specifies the string to search for. If this argument is a number, it will search for the character matching the ASCII value of the number.
Note: This function is case-insensitive.

Example:
Code:

  $website_name = 'WWW.PHPINTERVIEWQUESTION.COM';
  echo stristr($website_name,'w');

Output:

WWW.PHPINTERVIEWQUESTION.COM

Article written by admin

strrpos()

March 28, 2011 | Filed Under PHP Tutorial, String Functions | Comments Off

This function find position of last occurrence of a character in a string.

Syntax: strrpos(string,find,start)

Description:

The first argument “string” is Required. It specifies the string to search.
The second argument “find” is Required. It specifies the string to find.
The third argument “start” is Optional. It specifies where to begin the search.

Note: The strrpos() function is case-sensitive.

Example:
Code:

$str="download";
   echo strrpos($str,"o");

Output:
5

Article written by admin

strpbrk()

March 28, 2011 | Filed Under PHP Tutorial, String Functions | Comments Off

This function Search a string for any of a set of characters.

Syntax: strpbrk(string,charlist)

Description:

The first argument “string” is Required. It specifies the string to search.
The first argument “charlist” is Required It specifies the characters to find.

Example1:
Code:

$text = 'Get The Message.';
echo strpbrk($text, 'eM')."
";

Output:
et The Message.

Explanation:
This echoes “et The Message.” because ‘e’ is matched first.

Example2:
Code:

echo   strpbrk($text, 'T');

Output:
The Message.
Explanation:
This echoes “The Message.” because chars are case sensitive.

Article written by admin

substr()

March 25, 2011 | Filed Under PHP Tutorial, String Functions | Comments Off

This function Return part of a string.

Syntax: substr ( string,start , length )

Description:

The first argument “string”is Required.It specifies the string to return a part of

The second argument “start” is Required.Specifies where to start in the string
* A positive number – Start at a specified position in the string
* A negative number – Start at a specified position from the end of the string
* 0 – Start at the first character in string

The third argument “length” is Optional. It specifies the length of the returned string. Default is to the end of the string.

* A positive number – The length to be returned from the start parameter
* Negative number – The length to be returned from the end of the string

Example1:
Code:

echo substr('pqrstu', 1)."
"; echo substr('pqrstu', 1, 3)."
"; echo substr('pqrstu', 0, 4)."
"; echo substr('pqrstu', 0, 8)."
"; echo substr('pqrstu', -1, 1)."
";

Output:

qrstu
qrs
pqrs
pqrstu
u

You can access single charactere in a string using “curly braces”.

Example2:
Code:

$string = 'pqrstu';
echo $string{0}."
"; echo $string{3}."
"; echo $string{strlen($string)-1}."
";

Output:

p
s
u

Using a negative start

Example3:
Code:

 echo $str1 = substr("pqrstu",  -1)."
"; echo $str1 = substr("pqrstu", -2)."
"; echo $str1 = substr("pqrstu", -3, 1)."
";

Output:
u
tu
s
Using a negative length
Example4:
Code:

  echo $str1 = substr("pqrstu", 0, -1)."
"; echo $str1 = substr("pqrstu", 2, -1)."
"; echo $str1 = substr("pqrstu", 4, -4)."
"; echo $str1 = substr("pqrstu", -3, -1);

Output:

pqrst
rst

st

Article written by admin

md5_file()

March 25, 2011 | Filed Under PHP Tutorial, String Functions | Comments Off

This function Calculates the md5 hash of a given file.
It uses the RSA Data Security, Inc. MD5 Message-Digest Algorithm, and returns that hash. The hash is a 32-character hexadecimal number.
Syntax: md5_file(file,raw)
Description:
the first argument “file” is Required. The file to be calculated.
the second argument “raw” is Optional. It specifies hex or binary output format:
* True : Raw 16 character binary format
* False : Default. 32 character hex number
Note: This argument was added in PHP 5.0
Example:
Code:

$filename = "test.txt";
$md5file = md5_file($filename);
echo $md5file;

Output:
d41d8cd98f00b204e9800998ecf8427e

Article written by admin

md5()

March 25, 2011 | Filed Under PHP Tutorial, String Functions | Comments Off

This function Calculate the md5 hash of a string.
It uses the RSA Data Security, Inc. MD5 Message-Digest Algorithm, and returns that hash.
Syntax: md5(string,raw)
Description:
The first argument “string” is Required. The string to be calculated.
The second argument “raw” is Optional. It specifies hex or binary output format:
* TRUE – Raw 16 character binary format
* FALSE – Default. 32 character hex number
Note: This parameter was added in PHP 5.0
Example:
Code:

echo md5("Mumbai");
if(md5("Mumbai")== "8ebaad59980c95ade797c903761fa815")
{
echo  "Right";
}
else
{
echo "wrong";
}

Output:
8ebaad59980c95ade797c903761fa815

Article written by admin

str_word_count()

March 25, 2011 | Filed Under PHP Tutorial, String Functions | Comments Off

This function counts the number of words used in a string.

Syntax: str_word_count(string,format,char)

Description:

The first argument “string” is Required. It specifies the string to check.
The second argument “format” is Optional. It Specifies the return value of the str_word_count() function.
The following Possible values are:
* 0 – Default. Returns the number of words found.
* 1 – Returns an array with the words from the string.
* 2 – Returns an array where the key is the position of the word in the string, and value is the actual word.
The third argument “char” is Optional. It specifies special characters to be considered as words.

Example:
Code:

$str  = "All that glitters is not gold";
echo str_word_count($str)."
"; print_r(str_word_count($str,1)); print_r(str_word_count($str, 2)); $str1="Tom & Jerry"; print_r(str_word_count($str1,1)); print_r(str_word_count($str1,1,"&"));

Output:
6
Array (
[0] => All
[1] => that
[2] => glitters
[3] => is
[4] => not
[5] => gold
)
Array (
[0] => All
[4] => that
[9] => glitters
[18] => is
[21] => not
[25] => gold
)
Output:
str_word_count() without and with the char parameter:

Array (
[0] => Tom
[1] => Jerry
)
Array (
[0] => Tom
[1] => &
[2] => Jerry
)

Article written by admin

str_shuffle()

March 25, 2011 | Filed Under PHP Tutorial, String Functions | Comments Off

This function Randomly shuffles all the character of a string.
Syntax: str_shuffle ( string )

Description:

The argument “string” is Required. It specifies the string to shuffle.
Example:
Code:

$str = "Navin";
$shuffled= str_shuffle($str);
echo $shuffled;

Note: The Output changes each times.
Output:

nNvia

Article written by admin

str_split()

March 25, 2011 | Filed Under PHP Tutorial, String Functions | Comments Off

This function Convert a string to an array.
Syntax: str_split ( string , length )
Description:
The first argument “string” is Required. It specifies the string to split.
The second argument “length” is Optional. It specifies the length of each array element, otherwise each chunk will be one character in length.
Example:
Code:

$str  = "Delhi Times";
$arr1  = str_split($str);
$arr2 = str_split($str, 3);
print_r($arr1);
print_r($arr2);

Output:
Array (
[0] => D
[1] => e
[2] => l
[3] => h
[4] => i
[5] =>
[6] => T
[7] => i
[8] => m
[9] => e
[10] => s
)
Array (
[0] => Del
[1] => hi
[2] => Tim
[3] => es
)

Article written by admin

strip_tags()

March 25, 2011 | Filed Under PHP Tutorial, String Functions | Comments Off

This function removes HTML and PHP tags from a string.
Syntax:strip_tags(string,allow)
Description:
The first argument “string” is Required. It specifies the string to check.
The second argument “allow” is Optional. These tags will not be removed.
Example1:
Code:

$name  = "My name is Shashikant Pandey";
echo strip_tags($name);

Output:
My name is Shashikant Pandey

Example2:
Code:

$name = "My name is Shashikant Pandey";
echo strip_tags($name,"");

Output:
My name is Shashikant Pandey

Article written by admin

ord()

March 25, 2011 | Filed Under PHP Tutorial, String Functions | Comments Off

This function Returns the ASCII value of the first character of string.
Syntax: ord(string).
Description:
The argument “string” is Required. It gives the Ascii value of first character of the string.
Example:
Code:

$str = "A";
echo ord($str)."
"; $str1="Nokia"; echo ord($str1);

Output:
65
78

Article written by admin

nl2br()

March 25, 2011 | Filed Under PHP Tutorial, String Functions | Comments Off

This function Inserts HTML line breaks before all newlines in a string.
Syntax: nl2br ( string )

Description:

The argument “string” is Required. It specifies the string to check.
Example:
Code:

echo  nl2br("Abhishek is brand ambassador of\n Idea.");

Output:

Abhishek is brand ambassador of
Idea.

Article written by admin

© PHPInterviewQuestion.com 2009 - 2012

eXTReMe Tracker