![Page 1: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/1.jpg)
Chapter - 7
String & Patterns( Built-in Function )
PHP: Hypertext Preprocessor
![Page 2: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/2.jpg)
Outlineo What is String ?o Basic String o Using String As Arrayso Transforming Stringo Comparing Stringso Searching Stringso Matching Against Masko String Replace o Other String Functiono Arrays Functions
![Page 3: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/3.jpg)
What is String ?
o A string is series of characters, where a character is
the same as a byte. This means that PHP only supports
a 256-character set, and hence does not offer native
Unicode support. See details of the string type.
o A string literal can be specified in four different ways:
single quoted
double quoted
heredoc syntax
nowdoc syntax (since PHP 5.3.0)
![Page 4: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/4.jpg)
Basic Strings
String objects are a special type of container,
specifically designed to operate with sequences of
characters.
A string variable is used to store and manipulate
text.
String variables are used for values that contain
characters.
![Page 5: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/5.jpg)
Basic Strings
We using the string to display some notation .
The string, like a variable, will have to be created first.
There are two ways to use a string in PHP - you can store it
in a function or in a variable. In the example below, we will
create a string twice - the first time storing it in a variable,
and the second time - in a function, in our case - an echo.
![Page 6: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/6.jpg)
Basic Strings
<?php
$myString = "This is a string!";
echo "This is a string!";
echo $myString;
?>
![Page 7: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/7.jpg)
Use some different functions &
operators to manipulate the
string.
![Page 8: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/8.jpg)
Using String As Arrays
You can access the individual characters of
a strings as if they were members of an
array.
$text = “abcdef”;a b c d e f
0 1 2 3 4 5
Characters
Index
![Page 9: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/9.jpg)
Using String As Arrays
<?php
$str = “abcdef”;
echo $str[‘1’] ; // b
//Or
for($i =0 ;$i < srtlen($str); $i++)
{
echo $str[$i];
}
?>
![Page 10: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/10.jpg)
String Function
In PHP we have tow type of functions :
1) User-defined Function.
2) Built-in Function.
User-defined Function : is the function created
by user .
Built-in Function : is the function created by
PHP , and ready to use.
The real power of PHP comes from its functions.
We just talk about Built-in Function in this
chapter
![Page 11: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/11.jpg)
Transforming String
If given two arguments, the second should be
an array in the form array('from' => 'to', ...).
The return value is a string where all the
occurrences of the array keys have been replaced
by the corresponding values. The longest keys will
be tried first. Once a substring has been replaced,
its new value will not be searched again.
string strtr ( string $str , array $replace_pai
rs )
![Page 12: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/12.jpg)
Transforming String
<?php
echo strtr("Hilla
Warld","ia","eo");
?> Output : Hello World
![Page 13: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/13.jpg)
Transforming String
The strtr() If given three arguments, this function
returns a copy of str where all occurrences of
each (single-byte) character in from have been
translated to the corresponding character in to,
i.e., every occurrence of $from[$n] has been
replaced with $to[$n], where $n is a valid offset
in both arguments.;
string strtr ( string $str , string $from , stri
ng $to )
![Page 14: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/14.jpg)
Transforming String
<?php
$arr = array("Hello" => "Hi", "world" =>
"earth");
echo strtr("Hello world",$arr);
?>
Output : Hi earth
![Page 15: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/15.jpg)
Comparing Strings
You can comparison between strings by
more than one method ,the PHP when
comparison it make convert the data type
to other if the data type not the same.
![Page 16: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/16.jpg)
Comparing Strings
<?php
If(“123aa” == 123)
{
echo “done”;
}
?>
![Page 17: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/17.jpg)
Comparing Strings
This comparison is case sensitive.
Returns < 0 if str1 is less than str2; > 0
if str1 is greater than str2, and 0 if they
are equal.
int strcmp ( string $str1 , string $str2 )
![Page 18: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/18.jpg)
Comparing Strings
<?php
echo strcmp("Hello world!","Hello
world!");
?>Output : 0
![Page 19: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/19.jpg)
Comparing Strings
Binary safe case-insensitive string
comparison.
Returns < 0 if str1 is less than str2; > 0
if str1 is greater than str2, and 0 if they
are equal.
int strcasecmp ( string $str1 , string $str2 )
![Page 20: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/20.jpg)
Comparing Strings
<?php
$var1 = "Hello";
$var2 = "hello";
if (strcasecmp($var1, $var2) == 0) {
echo '$var1 is equal to $var2 in a case-
insensitive string comparison';
}
?>
![Page 21: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/21.jpg)
Comparing Strings-example
<?php
$str = "Ali";
if(strcmp($str , "ali") == 0)
{
//we won't get here , becuse of case sensitivity
}
if(strcasecmp($str , "ali") == 0)
{
we well get here becuse strcasecmp() is case-insensitive
}
?>
![Page 22: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/22.jpg)
Comparing Strings-example
<?php$st1 = “abcd1234”;$st2 = “abcd5678”;echo strncasecmp ($st1,$st2,4);
?>
![Page 23: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/23.jpg)
Searching Strings
• Find the numeric position of the first occurrence
of needle in the haystack string.
• Returns the position of where the needle exists
relative to the beginning of the haystack string
(independent of offset). Also note that string
positions start at 0, and not 1.
• Returns FALSE if the needle was not found.
int strpos ( string $haystack , $needle [, int $offset = 0 ] )
![Page 24: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/24.jpg)
Searching Strings
<?php
echo strpos("Hello world!","wo");
?>
Output : 6
![Page 25: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/25.jpg)
Searching Strings-Example
<?php$str =“abcdef”;$se_str = “abc”;If(strpos($str,$se_srt) !== false){ echo “found”;}
?>
![Page 26: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/26.jpg)
Searching Strings-Example
<?php$str ='1234567';$se_str = '123';echo strpos($str,$se_str,1); //false
?>
![Page 27: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/27.jpg)
Searching Strings-Example
<?php$str ='123456';$se_str = ‘34';echo strtr($str,$se_str,1); // 3456
?>
![Page 28: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/28.jpg)
Matching Against Mask
Finds the length of the initial segment of subject that
contains only characters from mask.
If start and length are omitted, then all of subject will be
examined. If they are included, then the effect will be the
same as callingstrspn(substr($subject, $start,
$length), $mask) (see substr for more information).
int strspn ( string $subject , string $mask [, int $start [, int $lengt
h ]] )
![Page 29: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/29.jpg)
Matching Against Mask
<?phpecho strspn("Hello world!","kHlleo");
?>
Output : 5
![Page 30: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/30.jpg)
Matching Against Mask
Returns the length of the initial segment
of str1 which does not contain any of the
characters in str2.
Returns the length of the initial segment
of subject which consists entirely of characters
in mask.
int strcspn ( string $str1 , string $str2 [, int $start [, int $length ]
] )
![Page 31: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/31.jpg)
Matching Against Mask
<?php
echo strcspn("Hello world!","w");
?>
Output :
6
![Page 32: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/32.jpg)
String Replace
This function returns a string or an array with all
occurrences of search in subject replaced with the
given replace value.
This function returns a string or an array with all occurrences of search in subject (ignoring case) replaced with the given replace value.
eplaces a copy of string delimited by the start and
(optionally) length parameters with the string given
inreplacement.
str_replace ( $search , $replace , $subject [, int &$count ] )
str_ireplace ( $search , $replace , $subject [, int &$count ]
)
substr_replace ( $string , $replacement , $start [, $length
] )
![Page 33: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/33.jpg)
String Replace-example
<?phpecho str_raplace(“Mr”,”Welcom”,”Mr
Ali”);
?>
Welcom Ali
![Page 34: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/34.jpg)
String Replace -exmple
<?php$a = 0;echo str_raplace(“a”,”b”,”a1a1a1”,$a);echo $a // 3
?>
![Page 35: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/35.jpg)
String Replace -exmple
<?phpecho substr_replace("welecom ali ","hi",7);
echo “<br>”;echo substr_replace("welecom my brother ali ","mr",9,10);
?> welecomhiwelecom mmrali
![Page 36: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/36.jpg)
String Replace -exmple
<?php$str = “123456789”;echo substr($str,0,3);//123echo substr($str,1,1);//2echo substr($str,-2);//89echo substr($str,-2,1);//8
?>
![Page 37: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/37.jpg)
String Functions
strtoupper() - Make a string uppercase
strtolower() - Make a string lowercase
ucfirst() - Make a string's first character
uppercase
ucwords() - Uppercase the first character
of each word in a string
![Page 38: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/38.jpg)
strtoupper()
<?php$str = “make a string uppercase ";$str = strtoupper($str);echo $str;
?>
MAKE A STRING UPPERCASE
![Page 39: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/39.jpg)
strtolower()
<?php$str = “MAKE A STRING LOWERCASE ";$str = strtolower($str);echo $str;
?>
make a string lowercase
![Page 40: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/40.jpg)
ucfirst()
<?php$str = “make a string's first character uppercase ";$str = ucfirst($str);echo $str;
?>
Make a string's first character uppercase
![Page 41: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/41.jpg)
ucwords()
<?php$str = “uppercase the first character of each word in a string ";$str = ucwords($str);echo $str;
?>Uppercase The First Character Of Each Word In A
String
![Page 42: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/42.jpg)
str_replace()
<?phpecho str_replace("world","Peter","Hello world!");
?>
Hello Peter!
![Page 43: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/43.jpg)
str_replace()
<?php$arr = array("blue","red","green","yellow");print_r(str_replace("red","pink",$arr,$i));echo "Replacements: $i";
?>Array ( [0] => blue [1] => pink [2] => green [3] => yellow )
Replacements: 1
![Page 44: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/44.jpg)
wordwrap()
Wraps a string to a given number of
characters using a string break character. Returns the given string wrapped at the
specified length.
![Page 45: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/45.jpg)
wordwrap()
<?php$text="The quick brown fox jumped over t
he lazy dog.";echo wordwrap($text,20,"<br/> \n");?>
![Page 46: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/46.jpg)
str_split ()
Converts a string to an array.
If the optional split_length parameter is specified, the
returned array will be broken down into chunks with each
being split_length in length, otherwise each chunk will be
one character in length.
FALSE is returned if split_length is less than 1. If
the split_length length exceeds the length of string, the
entire string is returned as the first (and only) array
element.
array str_split ( string $string [, int $split_lengt
h = 1 ] )
![Page 47: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/47.jpg)
str_split ()
<?php
$str = "Hello Friend";
$arr1 = str_split($str);$arr2 = str_split($str, 3);
print_r($arr1);print_r($arr2);
?>
![Page 48: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/48.jpg)
str_word_count()
Specify the return value of this function. The current
supported values are:
0 - returns the number of words found
1 - returns an array containing all the words found inside
the string
2 - returns an associative array, where the key is the
numeric position of the word inside the string and the
value is the actual word itself
str_word_count ( string $string [, int $format =
0 [, string $charlist ]] )
![Page 49: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/49.jpg)
str_word_count()
<?php
$str = "Hello fri3nd, you're looking good today!";
print_r(str_word_count($str, 1));print_r(str_word_count($str, 2));print_r(str_word_count($str, 1, 'àáãç3'));
echo str_word_count($str);
?>
![Page 50: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/50.jpg)
explode ()
Returns an array of strings, each of which
is a substring of string formed by splitting
it on boundaries formed by the
string delimiter.
array explode ( string $delimiter , string $string [, int $limit ] )
![Page 51: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/51.jpg)
explode ()
<?php
// Example 1
$chars = “A B C D E F";
$pieces = explode(" ", $chars);
echo $pieces[0]; // A
echo $pieces[1]; // B
?>
![Page 52: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/52.jpg)
strrev()
Returns string, reversed.<?php
echo strrev("Hello world!"); // outputs "!dlrow olle
?>
![Page 53: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/53.jpg)
str_shuffle ()
shuffles a string. One permutation of all
possible is created.
![Page 54: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/54.jpg)
str_shuffle ()
<?php$str = 'abcdef';$shuffled = str_shuffle($str);
// This will echo something like: bfdaececho $shuffled;
?>
![Page 55: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/55.jpg)
rand ()
If called without the
optional min, max arguments rand() returns a
pseudo-random integer between 0
and getrandmax(). If you want a random number
between 5 and 15 (inclusive), for example,
use rand(5, 15)
int rand ( void )
int rand ( int $min , int $max )
![Page 56: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/56.jpg)
rand ()
<?phpecho rand() . "\n";echo rand() . "\n";
echo rand(5, 15);?>
![Page 57: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/57.jpg)
round ()
<?phpecho round(3.4); // 3echo round(3.5); // 4echo round(3.6); // 4echo round(3.6, 0); // 4echo round(1.95583, 2); // 1.96echo round(1241757, -3); // 1242000echo round(5.045, 2); // 5.05echo round(5.055, 2); // 5.06
?>
round ( float $val [, int $precision = 0 [, int $mode = PHP_ROUND_HALF_UP ]] )
![Page 58: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/58.jpg)
round ()
<?phpecho round(9.5, 0, PHP_ROUND_HALF_UP); // 10echo round(9.5, 0, PHP_ROUND_HALF_DOWN); // 9echo round(9.5, 0, PHP_ROUND_HALF_EVEN); // 10echo round(9.5, 0, PHP_ROUND_HALF_ODD); // 9
echo round(8.5, 0, PHP_ROUND_HALF_UP); // 9echo round(8.5, 0, PHP_ROUND_HALF_DOWN); // 8echo round(8.5, 0, PHP_ROUND_HALF_EVEN); // 8echo round(8.5, 0, PHP_ROUND_HALF_ODD); // 9
?>
![Page 59: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/59.jpg)
max()
If the first and only parameter is an
array, max() returns the highest value in that
array. If at least two parameters are
provided, max()returns the biggest of these
values.
max ( array $values )
max ( $value1 , $value2 [, $value3... ] )
![Page 60: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/60.jpg)
max()
max() returns the numerically highest of the parameter
values. If multiple values can be considered of the same
size, the one that is listed first will be returned.
When max() is given multiple arrays, the longest array is
returned. If all the arrays have the same length, max() will
use lexicographic ordering to find the return value.
When given a string it will be cast as an integer when
comparing.
![Page 61: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/61.jpg)
max()
<?phpecho max(1, 3, 5, 6, 7); // 7echo max(array(2, 4, 5)); // 5
echo max(0, 'hello'); // 0echo max('hello', 0); // hello
echo max('42', 3); // '42'
echo max(-1, 'hello'); // hello
$val = max(array(2, 2, 2), array(1, 1, 1, 1)); // array(1, 1, 1, 1)
$val = max(array(2, 4, 8), array(2, 5, 7)); // array(2, 5, 7)
$val = max('string', array(2, 5, 7), 42); // array(2, 5, 7)?>
![Page 62: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/62.jpg)
min()
If the first and only parameter is an
array, min() returns the lowest value in that
array. If at least two parameters are
provided, min() returns the smallest of these
values.
min() returns the numerically lowest of the
parameter values.
min ( array $values )
min ( $value1 , $value2 [, $... ] )
![Page 63: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/63.jpg)
min()
?php
echo min(2, 3, 1, 6, 7); // 1
echo min(array(2, 4, 5)); // 2
echo min(0, 'hello'); // 0
echo min('hello', 0); // hello
echo min('hello', -1); // -1
?>
![Page 64: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/64.jpg)
str_pad ()
This functions returns the input string padded on the left,
the right, or both sides to the specified padding length. If
the optional argument pad_string is not supplied,
the input is padded with spaces, otherwise it is padded
with characters from pad_string up to the limit.
Returns the padded string.
string str_pad ( string $input , int $pad_length [, string $pad_string = " " [, int $pad_type =
STR_PAD_RIGHT ]] )
![Page 65: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/65.jpg)
str_pad ()<?php
$input = "Alien";
echo str_pad($input, 10); // produces "Alien
"
echo str_pad($input, 10, "-
=", STR_PAD_LEFT); // produces "-=-=-Alien"
echo str_pad($input, 10, "_", STR_PAD_BOTH); // produces
"__Alien___"
echo str_pad($input, 6 , "___"); // produces "Alien_“
?>
![Page 66: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/66.jpg)
str_repeat ()
Returns input repeated multiplier times.
<?php
echo str_repeat("-=", 10);
?>
string str_repeat ( string $input , int $multiplier )
![Page 67: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/67.jpg)
Array Functions
![Page 68: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/68.jpg)
array_push()
array_push () Push one or more elements
onto the end of array
Returns the new number of elements in
the array.
int array_push ( array &$array , $var [, mixed $... ] )
![Page 69: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/69.jpg)
array_push()
<?php
$stack = array("orange", "banana");
array_push($stack, "apple", "raspberry");
print_r($stack);
?>
![Page 70: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/70.jpg)
array_push()
array_pop — Pop the element off the end of array
Returns the last value of array. If array is empty (or is not
an array), NULL will be returned.
array_pop ( array &$array )
![Page 71: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/71.jpg)
array_pop()
<?php
$stack = array("orange", "banana", "apple", "raspberry");
$fruit = array_pop($stack);
print_r($stack);
?>
![Page 72: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/72.jpg)
array_merge()
array_merge — Merge one or more arrays
Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It
returns the resulting array.
If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the
arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
Values in the input array with numeric keys will be renumbered with incrementing keys starting from zero in the result array.
Returns the resulting array.
array array_merge ( array $array1 [, array $... ] )
![Page 73: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/73.jpg)
array_merge()
<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>
![Page 74: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/74.jpg)
array_rand ()
array_rand — Pick one or more random entries out of an array
Picks one or more random entries out of an array, and returns the key (or keys) of the
random entries.
If you are picking only one entry, array_rand() returns the key for a random entry. Otherwise,
it returns an array of keys for the random entries. This is done so that you can pick random
keys as well as values out of the array.
array_rand ( array $input [, int $num_req = 1 ] )
![Page 75: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/75.jpg)
array_rand ()
<?php
$bmw = array(“X2", “X3", “X4", “X5", “X6");
$rand_keys = array_rand($bmw, 2);
echo $bmw[$rand_keys[0]] . "\n";
echo $bmw[$rand_keys[1]] . "\n";
?
![Page 76: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/76.jpg)
array_replace ()
array_replace — Replaces elements from passed arrays into the first array
array_replace() replaces the values of the first array with the same values from all the following arrays. If a key from
the first array exists in the second array, its value will be replaced by the value from the second array. If the key exists in
the second array, and not the first, it will be created in the first array. If a key only exists in the first array, it will be left as
is. If several arrays are passed for replacement, they will be processed in order, the later arrays overwriting the previous
values.
array_replace() is not recursive : it will replace values in the first array by whatever type is in the second array.
Returns an array, or NULL if an error occurs.
array array_replace ( array $array , array $array1 [, array $... ] )
![Page 77: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/77.jpg)
array_replace ()
<?php
$base = array("orange", "banana", "apple", "raspberry");
$replacements = array(0 => "pineapple", 4 => "cherry");
$replacements2 = array(0 => "grape");
$basket = array_replace($base, $replacements, $replacements2);
print_r($basket);
?>
![Page 78: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/78.jpg)
range ()
range — Create an array containing a range of
elements
Returns an array of elements from start to limit,
inclusive.
array range ( mixed $start , mixed $limit [, number $step = 1 ] )
![Page 79: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/79.jpg)
range ()
<?php
$numbers = range(0, 12) ;// array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
foreach ($numbers as $num) { echo $num; }
// The step parameter was introduced in 5.0.0
$numbers = range(0, 100, 10) ;// array(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100)
foreach ( $numbers as $num) {echo $num; }
// Use of character sequences introduced in 4.1.0
$letters = range('a', 'i'); // array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i');
foreach ($letters as $let) { echo $let; }
$letters = range('c', 'a') // array('c', 'b', 'a');
foreach ($letters as $let) { echo $let; }
?>
![Page 80: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/80.jpg)
shuffle ()
This function shuffles (randomizes the order of
the elements in) an array.
Returns TRUE on success or FALSE on failure.
bool shuffle ( array &$array )
![Page 81: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/81.jpg)
shuffle ()
<?php
$numbers = range(1, 20);
shuffle($numbers);
foreach ($numbers as $number) {
echo "$number ";
}
?>
![Page 82: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/82.jpg)
asort ()
asort — Sort an array and maintain index
association
This function sorts an array such that array
indices maintain their correlation with the array
elements they are associated with. This is used
mainly when sorting associative arrays where the
actual element order is significant.
Returns TRUE on success or FALSE on failure.
bool asort ( array &$array [, int $sort_flags = SORT_REGULAR ] )
![Page 83: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/83.jpg)
asort ()
<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" => "b
anana", "c" => "apple");
asort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val\n";
}
?>
![Page 84: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/84.jpg)
usort ()
usort — Sort an array by values using a user-defined
comparison function
This function will sort an array by its values using a user-
supplied comparison function. If the array you wish to sort
needs to be sorted by some non-trivial criteria, you should
use this function.
Returns TRUE on success or FALSE on failure.
bool usort ( array &$array , callable $cmp_function )
![Page 85: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/85.jpg)
usort () <?php
function cmp($a, $b)
{
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
$a = array(3, 2, 5, 6, 1);
usort($a, "cmp");
foreach ($a as $key => $value) {
echo "$key: $value\n";
}
?>
![Page 86: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/86.jpg)
ksort ()
Sorts an array by key, maintaining key to data
correlations. This is useful mainly for associative
arrays.
Returns TRUE on success or FALSE on failure.
bool ksort ( array &$array [, int $sort_flags = SORT_REGULAR ] )
![Page 87: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/87.jpg)
ksort ()
<?php
$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c
"=>"apple");
ksort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val\n";
}
?>
![Page 88: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/88.jpg)
array_fill ()
Fills an array with num entries of the
value of the value parameter, keys
starting at
the start_index parameter.
Returns the filled array.
array array_fill ( int $start_index , int $num , mixed $value )
![Page 89: Outline o What is String ? o Basic String o Using String As Arrays o Transforming String o Comparing Strings o Searching Strings o Matching Against Mask](https://reader038.vdocument.in/reader038/viewer/2022110209/56649e535503460f94b48e66/html5/thumbnails/89.jpg)
array_fill()
<?php
$a = array_fill(5, 6, 'banana');
$b = array_fill(-2, 4, 'pear');
print_r($a);
print_r($b);
?>