substr()
function helps you to access a substring between given start and end points of a string. It can be useful when you need to get at parts of fixed format strings.
This function returns the part of the string as an output.
Syntax:
substr(<string>,<start>,[<length>]);
Explanation :
String : It is mandatory parameter. The string from which the part is to be extracted is mentioned here.
Start : The start in the string from which the characters are to be extracted
Length : It is an optional parameter. It specifies the length of the string which is to be extracted.
Example 1:
<?php echo substr("Hello world",6); ?> /* Output : world */
Example 2 :
<?php echo substr("Hello world",6,4); ?> /* Output : worl */
Example 3 :
<?php echo substr("Hello world", -1); ?> /* Output : d */
Example 4:
<?php echo substr("Hello world", -3, -1); ?> /* Output : rl */
The strlen()
function returns the length of the string.
Syntax :
strlen(<string>);
Explanation:
String : It is mandatory field. The string whose length is to be found out is mentioned here.
Example 1:
<?php echo strlen("Hello world"); ?> /* Output : 11 */
The trim()
function removes the whitespaces from both start and the end of the string.
Syntax :
trim(<string>);
Explanation :
String : It is mandatory field. The string of which the whitespaces are to be removed is passed as parameter.
Example 1:
<?php echo trim( " Hello World "); ?> /* Output : Hello World // If you go view source then you can see that there are no whitespaces. */
The ltrim()
function removes the whitespaces from the left part of the string.
Syntax :
ltrim(<string>);
Explanation :
String : It is mandatory field. The string of which the whitespaces are to be removed from left side is passed as parameter.
Example 1:
<?php echo ltrim( " Hello World "); ?> /* Output : Hello World // If you go view source then you can see that there are no whitespaces on // left side but there are spaces on right side. */
The rtrim()
function removes the whitespaces from the right part of the string.
Syntax :
rtrim(<string>);
Explanation :
String : It is mandatory field. The string of which the whitespaces are to be removed from right side is passed as parameter.
Example 1:
<?php echo rtrim( " Hello World "); ?> /* Output : Hello World // If you go view source then you can see that there are no whitespaces // on right side but there are spaces on left side */
The strtolower()
function converts the string to lower case.
Syntax :
strtolower(<string>);
Explanation :
String : It is mandatory field. The string which is to be converted to lower case is passed here.
Example 1:
<?php echo strtolower("HELLO WORLD"); ?> /* Output : hello world */
The strtoupper()
function converts the string to upper case.
Syntax :
strtoupper(<string>);
Explanation :
String : It is mandatory field. The string which is to be converted to upper case is passed here.
Example 1:
<?php echo strtoupper("hello world"); ?> /* Output : HELLO WORLD */
The str_replace()
function replaces some characters with some other characters in a string.
This function works by the following rules:
Syntax :
str_replace(<search>,<replace>,<string/array>,[<count>]);
Explanation :
Search : It is mandatory . The string or value to be searched comes here.
Replace : It is mandatory. The string or value to be replaced comes here.
String/Array : It is mandatory. The string or array in which the value is to be found out comes here.
Count : It is optional. It counts the number of replacements to be done.
Example 1:
<?php echo str_replace("world","Peter","Hello world"); ?> /* Output : Hello Peter */
Example 2:
<?php $arr = array("blue","red","green","yellow"); print_r(str_replace("red","pink",$arr,$i)); echo "Replacements: $i" ?> /* Output : Array ( [0] => blue [1] => pink [2] => green [3] => yellow ) Replacements: 1 */
Example 3:
<?php $phrase = "You should eat fruits, vegetables, and fiber every day." $healthy = array("fruits", "vegetables", "fiber"); $yummy = array("pizza", "beer", "ice cream"); $newphrase = str_replace($healthy, $yummy, $phrase); ?> /* Output : You should eat pizza, beer, and ice cream every day */
The strcmp()
function compares two strings.
This function returns:
Syntax :
strcmp(<string1>,<string2>);
Explanation :
String1 : It is mandatory. The first string comes here.
String 2 : It is mandatory. The Second string comes here.
Example 1:
<?php echo strcmp("Hello world!","Hello world!"); ?> /* Output : 0 */
Note: The strcmp()
function is binary safe and case-sensitive. For case insensitive comparison you can use strcasecmp(<string1>,<string2>);
function. It is similar to strcmp() function.
The explode()
function breaks the string into array on the basis of delimiter passed.
Syntax:
explode(<delimeter>,<string>,[<limit>]);
Explanation:
Delimeter: It is mandatory field. It specifies where to break the string.
String: It is mandatory. It specifies the string to split.
Limit : It is optional. It specifies the maximum number of array elements to return.
Example 1:
<?php $str = "Hello world. It's a beautiful day." print_r (explode(" ",$str)); ?> /* Output : Array ( [0] => Hello [1] => world. [2] => It's [3] => a [4] => beautiful [5] => day. ) */
The implode()
function join array elements with a string on the basis of delimiter passed.
Syntax:
implode(<delim>,<array>);
Explanation:
Delimiter: It is mandatory field. It specifies what to put between the array elements. Default is “” (an empty string).
Array: It is mandatory field. It specifies the array to join to a string.
Example 1:
<?php $arr = array('Hello','World!','Beautiful','Day!'); echo implode(" ",$arr); ?> /* Output: Hello World! Beautiful Day! */
Technical Architect
Kuala Lumpur, Malaysia
I'm a Full Stack Web Developer working in a MNC and passionate of developing modern web and mobile applications.
I have designed and developed CodephpOnline & CodephpOnline Wiki platform to expatiate my coding and technology learning experiences.
In my leisure time, I write technical articles on web development such as PHP, Laravel, CodeIgniter, Mediawiki, Linux, Angular, Ionic, ReactJS, NodeJS, AJAX, jQuery, Cloud and more.