Loading...
PHP

Strings in PHP

  1. strlen(): This function is used to get the length of string.
    <?php
    $str=”Welcome”
    echo strlen($str);
    ?>
    Output: 7
  2. ucwords:  This function is used to convert the first characters of all words in a string into uppercase .
    <?php
    $str=”welcome To Webnoid Schools”;
    echo ucwords($str);
    ?>
    Output: Welcome To Webnoid Schools
  3. ucfirst: This function is used to convert the first character of a string to character.
    <?php
    $str=”welcome to webnoid schools”;
    echo ucfirst($str);
    ?>
    Output: Welcome to webnoid schools
  4. nl2br: This function is used to break the new lines of a string.
    <?php
    echo nl2br(“Welcome \n to \n Webnoid schools”);
    ?>
  5. ord: This function is used to get ASCII value if input control.
    <?php
    echo ord(‘a’);
    ?>
  6. chr: This function is used to get the character of specified ASCII value.
    <?php
    echo chr(97);
    ?>
  7. strtoupper: This function is used to convert all characters of a string into upper case.
    <?php
    $str=”webniod”;
    echo strtoupper($str);
    ?>
  8. strtolower: This function is used to convert all character of string into lower case.
    <?php
    $str=”WEBNOID”;
    echo strtolower($str);
    ?>
  9. str_word_count: This is used to get the total number of words in a string.
    <?php
    $str=”Welcome to Webnoid Schools”;
    echo str_word_count($str);
    ?>
  10. str_repeat: This function is used to repeat a string with specific (interval) number of times.
    <?php
    $str=”Webnoid Schools”;
    echo str_repeat($str,5);
    ?>
  11. str_replace: This function is used to replace a string with a new string.
    <?php
    $str=”Webnoid Schools”;
    echo str_replace(“Webnoid Schools”, “Webnoid Soft Solutions”, $str);
    ?>
  12. similar_text: By this function we can get similarities between two strings.
    <?php
    $str1=”Webnoid”;
    $str2=”Webnoid”;
    echo similar_text($str1,$str2);
    ?>
  13. trim: By using this we can remove the left hand side and right hand side spaces of a string.
    i) ltrim: To remove the left hand side space of a string.
    ii)rtrim: To remove the right hand side space of a string.
    <?php
    $str1=”Webnoid”;
    $str2=”Schools”;
    echo $str1.trim($str2);
    ?>
  14. chop:  It is same as rtrim i.e., it removes right hand side of a string.
  15. strchr: This function is used to get a string from specified character of a string.
    <?php
    $str=”Webnoidschools”;
    echo strchr($str,”d”);
    ?>
  16. strrchr: This function is used to get sub string from a string based on specific character it searches for the specific character from reverse direction of a string.
    <?php
    $str=”Webnoidschools”;
    echo strrchr($str,’d’);
    ?>
  17. stristr: It is same as strchr but it is case insensitive.
    <?php
    $str=”Webnoidschools”;
    echo stristr($str,’D’);
    ?>
  18. strstr: This is same as strchr but it searches based on specific string .
    <?php
    $str=”Webnoid Schools”;
    echo strstr($str, “Schools”);
    ?>
  19. substr: To get sub string of a string.
    <?php
    $str=”Webnoidschools”;
    echo substr($str,2,4);
    ?>
  20. strcmp:  This function is used to compare two strings it is case sensitive. If both strings are same then it returns 0. If first string is is greater than second string it returns >0, if first string is less than than second then it returns <0.
    <?php
    $str1=”Webnoid”;
    $str2=”webnoid”;
    echo strcmp($str1,$str2);
    ?>
  21. strcasecmp: This is same as strcmp but it is case insensitive.

Leave a Reply

Your email address will not be published. Required fields are marked *