What is the string function in PHP? Describe with examples.

In this tutorial, we will learn how to arrange strings based on our requirements using the PHP string function. In this blog post, we will discuss different types of functions that are used in PHP to arrange a number sting or a sentence.

1. sprintf()

Using this function, we can represent a number with zero.

Example: 


<?php
    $number = 23;
    $new_number = sprintf( '%011d', $number );
    echo $new_number;
?>

 Output: 00000000023


2. strip_tags()

Remove HTML(<p></p>) tags from a string, like CK Editor output data.

Example:


<?php
    $data = "<p>This is an example text</p>";
    $new_data = strip_tags($data);
    echo $data;
?>

Output: This is an example text


3. substr()

This limit function was used to print a string with a character limit using a range.

Example:


<?php
    $data = "This is an example text, for string limit";
    $new_data = substr($data,0,10);
    echo $new_data;
?>

Output: This is an


4. str_replace()

This string is a function used to replace a string with a new one based on a particular string.

Example:


<?php
    $new_data = str_replace("text","content","This is an example text, for string replace");
    echo $new_data;
?>

Output: This is an example content, for string replace


5.ucwords()

This string is a function used to convert the first character of all words to uppercase in a string.

Example:


<?php
    $new_data = ucwords("This is an example text");
    echo $new_data;
?>

Output: This Is An Example Text

6. ucfirst()
This string is a function used to convert the first character of a string to uppercase.
Example:

<?php
    $new_data = ucfirst("this is an example text");
    echo $new_data;
?>

Output: This is an example text

7. strtolower()
This string is a function used to convert all the characters of a string to lowercase.
Example:

<?php
    $new_data = strtolower("This is An EXample Text");
    echo $new_data;
?>

Output: this is an example text

8. strtoupper()
This string is a function used to convert all the characters of a string to uppercase.
Example:

<?php
    $new_data = strtoupper("This is An EXample Text");
    echo $new_data;
?>

Output: THIS IS AN EXAMPLE TEXT

9. trim()
This string is a function used to remove spaces from both sides of a string.
Example:

<?php
    $new_data = trim(" This is an example text ");
    echo $new_data;
?>

Output: This is an example text

10. wordwrap()
This string is a function used to add a string when it reaches a specific length. We can use this function for line breaks, etc.
Example: 

<?php
    $new_data = wordwrap("This is an example text",15,"<br>\n");
    echo $new_data;
?>

Output: This is an
              example text

11. explode()
This string is a function used to break a string into an array based on a separator.
Example

<?php
    $new_data = explode(",","This is an example text, for array");
    print_r($new_data);
?>

Output: Array ( [0] => This is an example text [1] => for array )

12. implode()
This string is a function used to merge an array into a string based on a separator.
Example: 

<?php
    $array = array("This is an example text"," for array");
    $new_data = implode(",",$array);
    echo($new_data);
?>

Output: This is an example text, for array

13. str_shuffle()
This string is a function used to randomly shuffle all characters in a string.
Example:

<?php
    $new_data = str_shuffle("This is an example text");
    echo($new_data);
?>

Output: tt ee ixsmah lepTiasnx

14. str_word_count()
This string is a function used to count the total number of words in a string.
Example: 

<?php
    $new_data = str_word_count("This is an example text");
    echo($new_data);
?>

Output: 5

15. str_split()
This string is a function used to break a string into an array. Where each character is an array element.

Example:

<?php
    $new_data = str_split("Text");
    print_r($new_data);
?>

Output: Array ( [0] => T [1] => e [2] => x [3] => t )

I hope this tutorial will help you learn about the PHP string function to arrange a string based on your requirements.

Post a Comment

Previous Post Next Post