Debug School

rakesh kumar
rakesh kumar

Posted on

Explain different type of String Functions in php

Reference

  1. PHP String ucwords() Function
  2. PHP String ucfirst() Function
  3. PHP String substr_replace() Function
  4. PHP String substr_count() Function
  5. PHP string strcmp() Function
  6. PHP strlen() Function
  7. PHP string str_split() Function
  8. PHP explode() function
  9. PHP string Implode() Function
  10. PHP string join() Function
  11. PHP string parse_str() Function
  12. PHP string rtrim() Function

PHP String ucwords() Function

The ucwords() is an in-built function of PHP, which is used to convert the first character of each word to uppercase in a string. The ucwords() is supported by the PHP 4 and above versions. It takes a string as an input and converts the first character of each word of the string to uppercase. The other characters of the string remain the same.

Note: The ucwords() is a binary-safe function.
There are some other functions in PHP which are similar to the ucwords() function:

Related functions

strtoupper() - It converts a whole string into uppercase.
strtolower() - It converts a whole string into lowercase.
lcfirst() - It converts only the first character of a string into lowercase.
ucfirst() - It converts only the first character of a string into uppercase
Enter fullscreen mode Exit fullscreen mode

.
Syntax
The syntax of the ucwords() function is given below that accepts two parameters.

ucwords( $string, $separator)  
Enter fullscreen mode Exit fullscreen mode

The ucwords() returns the converted string whose first character of each word is converted to uppercase.

Parameters
$string (required) - It is a mandatory parameter of this function, which specifies the input string that needs to be converted.

$separator (optional) - It is an optional parameter of this function, which contains the words separator characters. It specifies a character that uses a separator for the words in the input string. By default these separator characters are:

Space
\t - tab
\n - newline
\r - carriage return
\f - form feed
\v - vertical tab
Enter fullscreen mode Exit fullscreen mode

Return Values
The ucwords() function returns the modified string, where the first character of each word in a string is converted to uppercase.

Changelog
Version Description
5.4.32, 5.5.16 $separator parameter added by these versions.
Examples
There are some examples given, through which we can learn the working of the ucwords() function. Let's see the below examples-

Example 1

<?php  
    $input_str = "hello, my name is lovyansh.";  
    echo ucwords($input_str);  
?> 
Enter fullscreen mode Exit fullscreen mode

Output:

Hello, My Name Is Lovyansh.

<?php  
    $input_str = "Good morning! everyone.";  
    echo "Before: ". $input_str;  
    echo "</br>";  
    $result_str = ucwords($input_str);  
    echo "After: ". $result_str;  
?> 
Enter fullscreen mode Exit fullscreen mode

Output:

Before: Good morning! everyone.
After: Good Morning! Everyone.
Enter fullscreen mode Exit fullscreen mode

Example 3

<?php  
    $input_str = "Good|morning!|everyone.";  
    $result_str1 = ucwords($input_str);  
    echo $result_str1. "</br>";  
    $result_str2 = ucwords($input_str, "|");  
    echo $result_str2;  
?> 
Enter fullscreen mode Exit fullscreen mode

Output:

In the above example, we have used "|" as a separator, which needs to be passed in ucwords() while modifying the string.

Good|morning!|everyone.
Good|Morning!|Everyone.
Enter fullscreen mode Exit fullscreen mode

PHP String ucfirst() Function

The ucfirst() function is used to convert the first character of the string into the uppercase. It is an in-built function of PHP, which takes a string as input and converts only the first letter of that string to uppercase. This function converts only the first character of the string to uppercase and others remains unchanged.

Note: PHP 4+ versions support this function.
There are some related functions in PHP which are similar to the strtolower() function:

Related functions

strtoupper() - It converts a whole string into uppercase.
strtolower() - It converts a whole string into lowercase.
lcfirst() - It converts only the first character of a string into lowercase.
ucwords() - It converts the first character of each word in a string into uppercase.
Enter fullscreen mode Exit fullscreen mode

Syntax
The syntax of the ucfirst() function is given below that contains only one string parameter.

ucfirst( $string) 
Enter fullscreen mode Exit fullscreen mode

The ucfirst() returns the converted string.

Parameter
$string (required) - It is a mandatory parameter of this function, which specifies the input string. It is the only argument, passed in the ucfirst() function.

Return Values
The ucfirst() function returns the converted string, where the first character of the string is converted to uppercase.

Examples
There are some examples given, through which we can learn the working of the ucfirst() function. Let's see the below examples-

Example

<?php      
    $strinn1 = "hey! myself shivani garg";  
    echo ucfirst($strinn1);  
?>
Enter fullscreen mode Exit fullscreen mode

Output:

As we can see in the below output that the ucfirst() converts only the first character of the string to uppercase.

Hey! myself shivani garg
Enter fullscreen mode Exit fullscreen mode

Example

<?php      
    $strinn1 = "hey! are you Aparna Garg?";  
    echo ucfirst($strinn1);  
    echo "</br>";  
    $strinn2 = "no, I'm Gunjan Garg.";  
    echo ucfirst($strinn2);   
?> 
Enter fullscreen mode Exit fullscreen mode

Output:

Hey! are you Aparna Garg?
No, I'm Gunjan Garg.
Enter fullscreen mode Exit fullscreen mode

PHP String substr_replace() Function

The substr_replace is an in-built function of PHP, which replaces a part of the string with another text within a string. PHP 4+ versions support this function. The index in the original string is passed as a parameter to which the replacement is to be performed. An array of string can also be passed as a parameter to this function, and in that case, a replacement will be done for each string at every turn (See Example 5).

Syntax
The syntax for the substr_replace() is given below, which accepts four arguments.

substr_replace( $string, $replacement, $start, $length)  
Enter fullscreen mode Exit fullscreen mode

Parameters
The substr_replace() consists of four parameters, in which three parameters are mandatory to pass in this function whereas $length parameter is optional. All these parameters are discussed below in detail:

$string (required) - It is the main string parameter of this function, which specifies the input string in which the replacement will be done. This is a mandatory parameter.

$replacement (required) - It is a mandatory parameter of the substr_replace(), which specifies the string to insert. The $replacement contains the string to which the replacement is going to be done.

$start (required) - It is also a mandatory parameter, which specifies the position from where the replacement needs to be started. This parameter holds the integer value. There are three conditions for this parameter:

If the value passed in $start is a positive number, then the replacement starts from the specified position in the string.
If the $start has a negative value, then the replacement starts from the specified position at the end of the string.
If the $start is 0, then the replacement will start from the first character of the string.
$length (optional) - It is an optional parameter of this function, which specifies how many characters, are to be replaced in the string. In case $length parameter is not passed in substr_replace(), then replacement stops at the end of the string. There are also three conditions for this parameter:

If the $length value is positive, then given length of the string will be replaced with the new string.
If the $length is negative, then the number of character should be left at the end after replacing the string.
If the $length is 0, then insertion is done instead of replacement.
Return Values
The substr_raplace() returns a string after the replacement is done. In case, if the string is an array, then it returns an array.

ChangeLog
From PHP 4.3.3, all the parameters accept arrays.

Examples
There are a few numbers of examples given, by which we can learn the practical implementation of the substr_replace() function.

Example 1

<?php  
    $replace = substr_replace("Hello earth", "javatpoint", 6);  
    echo $replace;  
?> 
Enter fullscreen mode Exit fullscreen mode

Output:

In the above example, replacement is started from 6th position. "earth" is replaced with "javatpoint".

Hello javatpoint
Enter fullscreen mode Exit fullscreen mode

Example 2

<?php  
    //replacement from the front of string  
    $replace1 = substr_replace("Hello javatpoint", "PHP", 6);  
    echo $replace1;  
    //replacement from end of the string  
    $replace2 = substr_replace("Hello javatpoint", "PHP", -10);  
    echo $replace2;  
?> 
Enter fullscreen mode Exit fullscreen mode

Output:

In the above example, replacement is started from 6th position in the first case. In the second case, a negative value is passed, so replacement is done at the end from 10th position in the string.

Hello PHP
Hello PHP
Enter fullscreen mode Exit fullscreen mode

Example 3

<?php  
    $replace = substr_replace("PHP!","Hello " , 0, 0);  
    echo $replace;  
?> 
Enter fullscreen mode Exit fullscreen mode

Output:

In the above example, we have used the fourth $length parameter, whose value is passed 0. Hence the insertion of string "Hello" is done rather than replacement.

Hello PHP!
Example 4

<?php  
    $replace = substr_replace("Welcome javatpoint","to " , 8, 0);  
    echo $replace;  
?> 
Enter fullscreen mode Exit fullscreen mode

Output:

Welcome to javatpoint
Example 5: Replacement multiple strings at once

<?php  
    $input_str =  array('X: AAAA', 'Y: AAAA', 'Z: AAAA');  

    //each AAA is replaced with string BBB.  
    echo implode('; ', substr_replace($input_str, 'BBBB',3,4)). '</br>';  

    $replace = array('XXXX', 'YYYY', 'ZZZZ');  
    //each AAA is replaced with different values.  
    echo implode('; ', substr_replace($input_str, $replace,3,4)). '</br>';  

    //Replacement of different number of character each time  
    $length = array(2,3,4);  
    echo implode('; ', substr_replace($input_str, $replace,3,$length)). '</br>';  
?> 
Enter fullscreen mode Exit fullscreen mode

Output:

X: BBBB; Y: BBBB; Z: BBBB
X: XXXX; Y: YYYY; Z: ZZZZ
X: XXXXAA; Y: YYYYA; Z: ZZZZ
Enter fullscreen mode Exit fullscreen mode

PHP String substr_count() Function

The substr_count() is a built-in function of PHP. It counts the number of times a substring occurs in the given string. The substr_count() is a case-sensitive function, which means it treats uppercase and lowercase alphabets differently.

For example - A substring "cha" is not present in the string "CHaru".

Note: The substr_count() is a case-sensitive function.
This function provides the facility to calculate that how many number of times a substring has occurred in the main string? It also provides an option to search a substring in a given range of the index. If the start and length values, which are specified for search is greater than the passed string, then it returns a warning to the user. PHP 4 and above versions support this function.

Note: The substr_count() function does not count overlapped substrings. (See the example 3).
Syntax
The syntax of the substr_count() is given below, which accepts four parameters, two string, and two integer type values.

substr_count($string, $substring, $start, $length) 
Enter fullscreen mode Exit fullscreen mode

Parameters
$string (required): The $string parameter is the main string parameter in which occurrence's of substring is counted. It is a mandatory parameter of substr_count() function.

$substring (required): The value passed in this parameter is searched in $string parameter and returns the counted occurrence of substring. It is also a mandatory parameter of substr_count() function.

$start (optional): This parameter consists of an integer value. It specifies that from where to start the counting. The $start is an optional parameter of this function. The value, which is passed in this parameter, is negative then it will start counting from the end of the string.

$length (optional): This parameter is an optional parameter and depends on $start parameter. The substr_count() will generate a warning, if $start and $length together ($start+$length) are greater than the length of $string. A negative length always counts from the end of the string.

Returns Value
The substr_count returns an integer means it returns the number of times a substring occurs in the main string.

Changelogs
In PHP 5.1.0, two new parameters were added, i.e., $start and $length.
From PHP 7.1.0, $start and $length supports negative value also. Length can be also 0 now.
Examples
Below some examples are given. With the help of these examples learn the practical implementation of substr_count() function in the program.

Example 1: Program without having optional parameters.

<?php   
    $strin = "Good Health Good Life";  
    $sub_str = "Good";  
    echo substr_count($strin, $sub_str);  
?>
Enter fullscreen mode Exit fullscreen mode

Output:

In the above example, "Good" substring is found 2 times in the main string.

2
Example 2: Program when $start parameter is passed.

<?php   
    $strin = "Good Health Good Life";  
    $sub_strin = "Good";  
    echo substr_count($strin, $sub_strin, 5);  
?>  
Enter fullscreen mode Exit fullscreen mode

Output:

In the above example, "Good" substring is found only once in the main string, as searching is started from 5th position.

1
Example 3: Program when $start and $length both parameters are passed.

<?php   
    $strin = "Good Health Good Life";  
    $sub_strin = "Good";  
    echo substr_count($strin, $sub_strin, 5, 10);  
?> 
Enter fullscreen mode Exit fullscreen mode

Output:

In the above example, "Good" substring is not found in the main string, because searching is started from 5th position to 15th ($start+$length).

0
Example 4: Case-sensitivity

<?php   
    $strin = "Good Health Good Life";  
    $sub_strin = "LIFE";  
    echo substr_count($strin, $sub_strin);  
?> 
Enter fullscreen mode Exit fullscreen mode

Output:

In the above example, "LIFE" substring is not found in the main string, because substr_compare() is a case-sensitive function.

0
Example 5:

<?php   
    $strin = "Hello javaworld";  
    $sub_strin = "Hello";  
    echo substr_count($strin, $sub_strin, 5,12);  
?> 
Enter fullscreen mode Exit fullscreen mode

Output:

A warning is occurred here, because start + length > strin, i.e., (5+12 > 15). The length of $start + $length parameter should not be exceeded the length of the main string.

Warning: substr_count(): Invalid length value in C:\xampp\htdocs\xampp\PMA\substr_count.php on line 4
Example 6: Overlapped Substring

<?php   
    $strin = "Hello javava";  
    $sub_strin = "java";  
    echo substr_count($strin, $sub_strin);  
?>
Enter fullscreen mode Exit fullscreen mode

Output:

It returned 1, as it didn't count the overlapped substring.

1

PHP string strcmp() Function

he strncmp() is predefine string function of PHP. It is used to compare two strings. This function strncmp() is binary-safe, case-sensitive and similar to strcmp() function but strcmp() have no the length parameter.

The strncmp () function is often used in programming and web development for string operation.

Syntax:

strncmp(string1,string2,length); 
Enter fullscreen mode Exit fullscreen mode

Parameter Description Required/Optional
String1 Specify the first string to compare Required
String2 Specify the second string to compare Required
Length Specify the number of character for comparison. Required
The strncmp () function:

Returns 0 : if the two string is equal.
Returns <0 : if string 1 is less than string2.
Return >0 : if string1 is greater than string 2.
Enter fullscreen mode Exit fullscreen mode

Example 1

<?php  
$str1 = "PHP javatpoint";  
$str2 = "PHP javatpoint";  
echo "By using 'strcmp()' function:".strcmp($str1, $str2);  
// it will return: 0 ( Both string is equall )  
 ?> 
Enter fullscreen mode Exit fullscreen mode

Output:

By using 'strcmp()' function: 0
Example 2

<?php  
 $str1 = "PHP javatpoint";  
$str2 = "PHP javatpoint tutorial";  
echo "By using 'strcmp()' function:".strcmp($str1, $str2);  
// it will return: -9 ( str2 is greater than str1)  
 ?> 
Enter fullscreen mode Exit fullscreen mode

Output:

By using 'strcmp()' function:-9
Example 3

<?php  
$str1 = "PHP javatpoint tutorial";  
$str2 = "PHP javatpoint";  
echo "By using 'strcmp()' function:".strcmp($str1, $str2);  
// it will return: 9 ( str1 is greater than str2)  
?>  
Enter fullscreen mode Exit fullscreen mode

Output:

See Also :
str_word_count() : It is used to count the number of words in a string.
strcasecmp() : It is used to compare two strings.
strchr() : It is used to find the first occurrence of a string inside another string.

PHP strlen() Function

The strlen() function is predefined function of PHP. It is used to find the length of string or returns the length of a string including all the whitespaces and special character.

Syntax:

strlen(string);
Enter fullscreen mode Exit fullscreen mode

Parameter Description Required/Optional
String Specify the string to check. Required.
Example 1

<?php  
$str = 'Javatpoint';  
echo "Your String is:".$str;  
echo "<br>";  
echo "By using 'strlen()' function:".strlen($str); // 10  
?>  
Enter fullscreen mode Exit fullscreen mode

Output:

Your String is:Javatpoint
By using 'strlen()' function:10
Enter fullscreen mode Exit fullscreen mode

Example 2

<?php  
$str = 'Java T point';  
echo "Your String is:".$str;  
echo "<br>";  
echo "By using 'strlen()' function:".strlen($str);   
// output =12 [including whitespace]  
?> 
Enter fullscreen mode Exit fullscreen mode

Output:

Your String is:Java T point
By using 'strlen()' function:12
Enter fullscreen mode Exit fullscreen mode

Example 3

<?php  
$str = 'P    H    P';  
echo "Your String is:".$str;  
echo "<br>";  
echo "By using 'strlen()' function:".strlen($str);   
// output =11 [including whitespace]  
?>  
Enter fullscreen mode Exit fullscreen mode
Output:

Your String is:P H P
Enter fullscreen mode Exit fullscreen mode

By using 'strlen()' function:11

PHP string str_split() Function
The str_split() is predefined function of PHP. It is used to convert a string to an array. If the split length is specified, then array will be broken down into chunks with length.

It will return FALSE if the split length is less than 1.

Syntax:

str_split(string,length); 
Enter fullscreen mode Exit fullscreen mode

Parameter Description Required/Optional
String Specify the string to split. Required
Length Specify the length of each array element.
Default is :1 Optional
Example 1

<?php  
$str="Hello PHP";  
print_r(str_split($str));  
?> 
Enter fullscreen mode Exit fullscreen mode

Output:

Array ( 
[0] => H 
[1] => e
 [2] => l 
[3] => l 
[4] => o 
[5] => 
[6] => P 
[7] => H 
[8] => P
 )
Enter fullscreen mode Exit fullscreen mode

Example 2

<?php  
$str="Hello PHP";  
print_r(str_split($str,3));  
?>  
Enter fullscreen mode Exit fullscreen mode
Output:

Array ( 
[0] => Hel 
[1] => lo 
[2] => PHP 
)
Enter fullscreen mode Exit fullscreen mode

PHP explode() function

PHP explode() is a string function, which splits a string by a string. In simple words, we can say that it breaks a string into an array. The explode() function has a "separator" parameter, which cannot contain an empty string, because it holds the original string that is to be split. It is a binary-safe function.

The explode() function returns an array of strings created by splitting the original string.

Syntax:

explode (string $separator, string $originalString, int $limit) 
Enter fullscreen mode Exit fullscreen mode

Parameters
There are three parameters passed in the explode() function, in which two parameters are mandatory, and the last one is optional to pass. These parameters are as follows:

$separator:

This parameter specifies the character for the point at which the original string will split. In simple words, we can say that whenever this character is found in the string, the string will be divided into parts.

$originalString:

This parameter holds the string which is to be split into array.

$limit:

The $limit parameter specifies the number of array elements to be returned. It can contain any integer value (zero, positive, or negative).

Possible values for $limit:

Positive
(Greater than 0) If this parameter contains a positive value, this function returns an array of string, split into a size defined in the $limit parameter.
Negative
(Less than 0) If the $limit parameter consists of the negative value, elements from the last will be trimmed, and the remaining will be returned.
Zero If zero (0) is passed in $limit parameter, it will return the whole string as a single array element.
Note: Remember that, if the $limit parameter is not provided in explode() function, the returned array will contain all elements of string separated by the $separator string.
Return Value
This function returns an array of strings. This array of string is formed by splitting the original string.

Changes
After PHP version 5.1.0, negative value is allowed to provide in $limit parameter.

Examples
Example 1: Array with $limit parameter

<?php    
    // original string   
    $Original_str = "Hello, we are here to help you.";     

    // Passed zero   
    print_r (explode (" ",$Original_str, 0));   
    // Passed positive value  
    print_r (explode (" ",$Original_str, 4));   
    // Passed negative value   
    print_r (explode (" ",$Original_str, -3));     
?>  
Enter fullscreen mode Exit fullscreen mode

Output:

In the above example, a space character is used as separator to split the string.

Array ( [0] => Hello, we are here to help you. ) Array ( [0] => Hello, [1] => we [2] => are [3] => here to help you. ) Array ( [0] => Hello, [1] => we [2] => are [3] => here )
Enter fullscreen mode Exit fullscreen mode

The above output can be seen as to better understand:

Array ( 
[0] => Hello, we are here to help you. 
) Array ( 
[0] => Hello, 
[1] => we 
[2] => are 
[3] => here to help you. 
) Array ( 
[0] => Hello, 
[1] => we 
[2] => are 
[3] => here 
)
Enter fullscreen mode Exit fullscreen mode

Example 1: Array without $limit parameter

<?php    
    // original string   
    $Original_str = "Hello, welcome to javatpoint.";     
    //without passing optional parameter  
    print_r (explode (" ", $Original_str));  
?> 
Enter fullscreen mode Exit fullscreen mode

Output:

In the above code, we do not pass the optional parameter, i.e., $limit. Therefore, the explode() function splits the string into array for different indexes.

Array ( [0] => Hello, [1] => welcome [2] => to [3] => javatpoint. )
Enter fullscreen mode Exit fullscreen mode

Example 3:

<?php    
    // original string   
    $Original_str = "Hello, welcome to javatpoint.";     
    //without passing optional parameter  
    print_r (explode ("e", $Original_str));  
?> 
Enter fullscreen mode Exit fullscreen mode

Output:

In the above code, we used the "e" character to split the string into an array. So, wherever the "e" is found, the string will be split.

Array ( [0] => H [1] => llo, w [2] => lcom [3] => to javatpoint. )
Enter fullscreen mode Exit fullscreen mode

PHP string Implode() Function

PHP implode() is a string function, which joins the array elements in a string. It is a binary-safe function. In implode() function, parameters can be passed in any order.

The implode() function works same as the join() function and returns a string created from the elements of the array. Basically, this function joins all elements of array in one string.

Syntax
There are two syntax are available for implode() function, which are given below:

implode (string $glue, array $pieces) 
Enter fullscreen mode Exit fullscreen mode

or

implode (array $pieces)
Enter fullscreen mode Exit fullscreen mode

Join the array elements by $glue string parameter.

Parameters
There are two parameters can be passed in the implode() function, one of which is mandatory, and another one is optional. These parameters are as follows:

$glue (optional):

It is an optional and string type of parameter. It contains the value to join the array elements and form a string. Basically, $glue is used to join the string.

$pieces (mandatory):

This parameter contains the array of string to implode. The array elements are mandatory to pass in implode() function to join into one string.

Return Value
The implode() function returns the string formed from the array elements. The string will be formed in the same order as elements passed into array. The return type of this function is string.

Changes
After PHP version 7.4.0, passing the $glue parameter after the $pieces parameter has been deprecated.

Examples
Example 1:

In the below example, array elements are joined with + operator using implode() function.

<?php    
    echo "Before using 'implode()' function: <br>";    
    echo "array('Welcome', 'to', 'PHP', 'tutorial') <br> <br>";  

    //store array element in a variable  
    $arr = array('Welcome', 'to', 'PHP', 'tutorial');    

    //join array elements in a string by + operator  
    echo "After using 'implode()' function: <br>";  
    echo implode("+",$arr);     
?> 
Enter fullscreen mode Exit fullscreen mode

Output:

Before using 'implode()' function:
array('Welcome', 'to', 'PHP', 'tutorial')

After using 'implode()' function:
Welcome+to+PHP+tutorial
Enter fullscreen mode Exit fullscreen mode

Example 2:

<?php    
    $input_arr = array ('Noida', 'Delhi', 'Gurugram');  
    //join with comma and space separator  
    $comma_separation = implode (", ", $input_arr);  
    echo $comma_separation;  

    echo "</br>";  
    //join without separator  
    print_r (implode ($input_arr));  
?> 
Enter fullscreen mode Exit fullscreen mode

Output:

Noida, Delhi, Gurugram
NoidaDelhiGurugram
Example 3:

In the below example, two arrays are joined together using implode() function.

<?php    
    $input_arr1 = array ('Hello', 'everyone!');  
    $input_arr2 = array ('One' => 'Welcome', 'Two' => 'to', 'Three' => 'Javatpoint');  

    //join both array elements  
    echo implode(' ', $input_arr1), ' / ', implode(' ', $input_arr2);  
?>  
Enter fullscreen mode Exit fullscreen mode
Output:

Hello everyone! / Welcome to Javatpoint
Enter fullscreen mode Exit fullscreen mode

PHP string join() Function

PHP string join() is predefined function. It is used to return a string from the elements of an array. It is an alias of the implode() function.

Syntax:
join(separator,array)

Parameter Description Required/Optional
separator Specify the array element optional
array The array to join to a string required
Example 1

<?php  
$arr = array('Hello','PHP','Join','Function');  
echo join(" ",$arr)."<br>";  
?> 
Enter fullscreen mode Exit fullscreen mode
Output:

Hello PHP Join Function
Enter fullscreen mode Exit fullscreen mode

Example 2

<?php  
$arr = array('Hello','PHP','Join','Function');  
echo join("+",$arr)."<br>";  
?> 
Enter fullscreen mode Exit fullscreen mode
Output:

Hello+PHP+Join+Function
Enter fullscreen mode Exit fullscreen mode

Example 3

<?php  
$arr = array('Hello','PHP','Join','Function');  
echo join("-",$arr)."<br>";  
?> 
Enter fullscreen mode Exit fullscreen mode
Output:

Hello-PHP-Join-Function
Enter fullscreen mode Exit fullscreen mode

Example 4

<?php  
$arr = array('1', '2', '3', '4', '5', '6', '7', '8', '9', '10');  
echo join('<br>', $arr);  
?>  
Enter fullscreen mode Exit fullscreen mode
Output:

1
2
3
4
5
6
7
8
9
10
Enter fullscreen mode Exit fullscreen mode

PHP string parse_str() Function

PHP string parse_str() is in-built function. It is used to parse the string into variables.

Syntax:
parse_str(string,array);

Parameter Description Required/Optional
String Specify the string to parse required
Array Specify the name of an array to store the variables. optional
Example 1

<?php  
parse_str("name=John&age=26",$yourArray);  
print_r($yourArray);  
?> 
Enter fullscreen mode Exit fullscreen mode

Output:

Array ( [name] => John [age] => 26 )
Enter fullscreen mode Exit fullscreen mode

Example 2

<?php  
parse_str("name=John&age=26");  
echo "Your Name:".$name."<br>";  
echo "Your Age:".$age;  
?>
Enter fullscreen mode Exit fullscreen mode

Output:

Your Name:John
Your Age:26
Enter fullscreen mode Exit fullscreen mode

Example 3

<?php  
parse_str("My Website=JavaTpoint");  
echo $My_Website; // JavaTpoint  
?> 
Enter fullscreen mode Exit fullscreen mode

Output:

JavaTpoint
Example 4

<?php  
parse_str("My Language=PHP", $output);  
echo $output['My_Language']; // Something*/  
?> 
Enter fullscreen mode Exit fullscreen mode

Output:

PHP

PHP string rtrim() Function

PHP rtrim() is predefined function which is used to remove whitespace or character form the right side of a string
Syntax:

rtrim(string,charlist);
Enter fullscreen mode Exit fullscreen mode

Example 1

<?php  
$str = "Hello PHP!    ";  
echo "Without rtrim() function: " . $str;  
echo "<br>";  
echo "With rtrim() function:" . rtrim($str);  
?> 
Enter fullscreen mode Exit fullscreen mode

Output:

Without rtrim() function: Hello PHP! 
With rtrim() function:Hello PHP!
Enter fullscreen mode Exit fullscreen mode

Example 2

<?php  
$str = "\n\n\nHello PHP!\n\n\n";  
echo "Without trim() function: ". $str;  
echo "<br>";  
echo "With trim() function: ". trim($str);  
?> 
Enter fullscreen mode Exit fullscreen mode

Output:

Without trim() function: Hello PHP! 
With trim() function: Hello PHP!
Enter fullscreen mode Exit fullscreen mode

Top comments (0)