Monday 29 September 2014

wordwrap in PHP

PHP strtok() function is utilized to split a string into smaller strings(tokens).

Syntax:

strtok(string,split)
string : Required. Specifies the string to split

split : Required. Specifies the string characters token delimiters.

Example:

<?php
$str_name = "Good morning. Have a nice day.";
$token_name = strtok($str_name," ");
while($token_name != false){
     echo "$token_name <br />";
     $token_name = strtok(" ");
}
?>
Output will be:
Good
morning.
Have
a
nice
day.
*****************************************************************
 PHP strtolower() function returns an all-lowercase string, regardless of whether the original was all uppercase or mixed.

Syntax:

strtolower(string)
string : Required. Specifies the input string.

Example:

<?php
echo strtolower("Good Morning World!");
?>
Output will be:
good morning world!
*****************************************************************
PHP strtoupper() function returns an all-uppercase string, regardless of whether the original was all lowercase or mixed.

 Syntax:

strtoupper(string)
Example:

<?php
 echo strtoupper("Good Morning World!");
?>
Output will be:
GOOD MORNING WORLD!
*****************************************************************
PHP strtr() function is utilized to interpret certain characters in a string.

Syntax:

strtr(string,from,to)
Or

strtr(string,array)
string : Required. Specifies the string to interpret.

from : Required (unless array is utilized). Holds characters to be translated.

to : Required (unless array is utilized). Holds characters to be translated with.

array : Required (to and from is used). An array holding 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.

Example:

<?php
echo strtr("Hoad Marning","Ha","Go");
$arr_name = array("Good" => "Good", "Morning" => "Night");
echo strtr("Good Morning",$arr_name);
?>
Output will be:
Good Morning
Good Night
*****************************************************************
PHP substr() function is utilized to get a part of a string from a string, beginning at a specified position.

Syntax:

substr(string,start,length)
string : Required.  Specifies the string to return a part of

start : Required.Specifies where to begin in the string.

A positive number – begin at a specified position in the string.
A negative number – begin  at a specified position from the end of the string.
 0 – begin  at the first character in string.
length : Optional. Specifies the length of the returned string. Default is to end of the string.

A positive number – The length to be returned from the start parameter.
 Negative number – he length to be returned from the finish of the string
Note: If begin is a negative number and length is less than or equal to begin, length gets 0.

Example:

<?php
$input_str  = "Good Morning!";
echo substr($input_str,5);
echo substr($input_str,5,7);
?>
Output will be:
Morning!
Morning
*****************************************************************
PHP substr_compare() function is utilized to analyze two strings from a specified starting position.

Function Returns :

 0 – If the two strings are equal
 <0 – If the string1 (from startpos) is less than string2
>0 – If the string1 (from startpos) is greater than string2
Syntax:

substr_compare(string1,string2,startpos,length,case)
string1 : Required. Specifies the first string being compared

string2 : Required. Specifies the second string being compared

startpos : Required. Specifies where to begin comparing in string1

length : Optional. Specifies how much of string1 to compare

case : Optional. Specifies whether or not to perform a case-sensitive compare. Default is FALSE (case-sensitive)

Tip : PHP substr_compare() function is binary safe and alternatively case-sensitive.

Example:

<?php
$input_str  = "Good Morning!";
echo substr_compare($input_str,"Good Morning!",0)."<br />";
echo substr_compare($input_str,"Morning!",5)."<br />";
echo substr_compare($input_str,"MORNING!",5,TRUE);
?>
Output will be:
0
0
0
*****************************************************************
 PHP substr_count() function will count the number of times a substring is found in a string.

Syntax:

substr_count(string,substring,start,length)
string : Required. Specifies the string to check

substring : Required. Specifies the string to search for

start : Optional. Specifies where to in string to begin searching

length : Optional. Specifies the length of the search

Example:

<?php
$input_str  = "Good morning. Have a nice day!";
echo substr_count($input_str,"nice");
?>
Output will be:
1
*****************************************************************
PHP substr_replace() function is utilized to swap a part of a string with another string or words.

Syntax:

substr_replace(string,replacement,start,length)
string : Required. Specifies the string to operate

replacement : Required. Specifies the string to replace

start : Required. Specifies where to start position of the substring to replace

A positive number – Start replacing at the specified position in the string
A negative number – Start replacing at the specified position from the end of the string
0 – Start replacing at the first character in string
 length : Optional. Specifies the length of the string segment to be replaced. Default is the same length as the string.

A positive number – The length of string to be replaced
Negative number – How many characters should be left at end of the string after replacing
0 – Insert instead of replace
Note : If begin is a negative number and length is less than or equivalent to begin, length gets 0.

Example:

<?php
$input_str  = "Good Morning!";
echo substr_replace($input_str,"Night",5);
?>
Output will be:
Good Night!
*****************************************************************
PHP trim() function is utilized to strip out white space (or different characters) from the beginning and end of a string.

Syntax:

trim(string, charlist)
string : Required. Specifies the string to check

charlist : Optional. Specifies which character to remove from the string.

If not specified all of the following characters will be removed :

"\0" – NULL
"\t" – tab
"\n" – new line
"\x0B" – vertical tab
"\r" – carriage return
" " – ordinary white space
Example:

<?php
$input_str= "\n\rGood Morning!\n\r";
echo "Without trim : ". $input_str;
echo "<br />";
echo "With trim : ". trim($input_str);
?>
Output will be:
Without trim : Good Morning!
 With trim : Good Morning!
If You Select "View Source" in the browser window, you will see the following HTML :

<html>
<body>
Without trim : 

Good Morning!

<br />With trim : Good Morning!
</body>
</html>
*****************************************************************
PHP ucfirst() function is capitalizes only the first letter of a string.

Syntax:

ucfirst(string)
string : Required. Specifies the string to convert

Example:

<?php
$input_str  = "good morning!";
echo ucfirst($input_str);
?>
Output will be:
Good morning!
*****************************************************************
PHP ucwords() function is capitalizes the first letter of each word in a string.

Syntax:

ucwords(string)
string : Required. Specifies the string to convert

Example:

<?php
$input_str  = "good morning!";
echo ucwords($input_str);
?>
Output will be:
Good Morning!
*****************************************************************
PHP vfprintf() function is used to write a formatted string to a specified output stream.

PHP vfprintf() function returns the length of the written string.

Syntax:

vfprintf(stream,format,argarray)
stream : Required. Specifies where to write/output the string

format : Required. 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)

Note : If multiple additional format values are used, they must be in the same order as above.
argarray : Required. An array with arguments to be inserted at the % signs in the format string.

Note : If there are more % signs than arguments, you must use placeholders. A placeholder is inserted after the % sign, and consists of the argument- number and "\$".

Tip : Related functions: fprintf(), printf(), sprintf(), vprintf(), and vsprintf().

Example:

<?php 
$input_str = "Good";
$input_number = 330;
$file_name = fopen("test.txt","w");
echo vfprintf($file_name,"%s Morning. It is day number %u",array($input_str,$input_number));
?>
Output will be:
34
The following text will be written to the file "test.txt":

Good Morning. It is day number 330
*****************************************************************
PHP vprintf () function is utilized to display array values as a formatted string.
The PHP vprintf () function Operates as printf() but accepts an array of arguments, instead of a variable number of arguments.

Syntax:

vprintf(stream,format,argarray)
stream : Required. Specifies where to write/output the string

format : Required. 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 put between the % and the letter (example %.2f) :

+ (Forces both + and -before numbers. By default, just negative numbers are marked)
'(Specifies what to utilize as padding. Default is space. Must be utilized together with the width specifier. Illustration: %'x20s (this utilization "x" as padding)
-(Left-supports the variable value)
[0-9] (Specifies the minimum width expected of to the variable value)
[0-9] (Specifies the number of decimal digits or most extreme string length)

Note : If multiple additional format values are utilized, they must be in the same request as above.
argarray : Required. An array with arguments to be inserted at the % signs in the format string

Note : If there are more % signs than arguments, you should use placeholders. A placeholder is inserted after the % sign, and consists of the argument number and "\$".

Tip : Related functions: fprintf(), printf(), sprintf(), vfprintf() and vsprintf().

Example:

<?php 
$input_str = "Good";
$input_number = 330;
echo vprintf("%s Morning. It is day number %u",array($input_str,$input_number));
?>
Output will be:
Good Morning. It is day number 330
*****************************************************************
vsprintf () function is utilized to write a formatted string to a variable.

The PHP vsprintf () function Operates as sprintf() but accepts an array of arguments, instead of a variable number of arguments.

Syntax:

vsprintf(stream,format,argarray)
stream : Required. Specifies where to write/output the string

format : Required. 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)
Note:  If various additional format values are utilized, they must be in the same request as above.
argarray : Required. A array with arguments to be embedded at the % signs in the format string.

Note : If there are more % signs than arguments, you should use placeholders. A placeholder is inserted after the % sign, and consists of the argument number and "\$".

Tip : Related functions: fprintf(), printf(), sprintf(), vfprintf(), and vprintf().

Example:

<?php 
$input_str = "Good";
$input_number = 330;
$res_txt = vsprintf($file_name,"%s Morning. It is day number %u",array($input_str,$input_number));
echo $res_txt;
?>
Output will be:
Good Morning. It is day number 330
*****************************************************************
PHP wordwrap() function is utilized to wraps a sentence into new lines using a string break character.

Syntax:

wordwrap(string,width,break,cut)
string : Required. Specifies the string to split into lines.

width : Optional. Specifies the greatest line width. Default is 75.

break : Optional. Specifies the characters to utilize as break. Default is "\n".

cut : Optional. Specifies if statements longer than the specified width should be wrapped. Default is FALSE (no-wrap).

Note : PHP wordwrap function may leave white spaces at the starting of a line.

Example:

<?php
$str = "Welcome to online php guide.";
$newtext= wordwrap($str,22,"<br />\n");
echo $newtext;
?>
Output will be:
Welcome to online php
guide.
*****************************************************************

0 comments:

Post a Comment