PHP trim Function



PHP Tutorial > String Functions > trim Function

The trim function in PHP is used to strip out characters at both ends of a string. The syntax of the trim function is:

trim ('string', 'character_list')

If character_list is not specified, white spaces are trimmed from the string. White spaces include the following:

  • " ": a space
  • "\t": a horizontal tab
  • "\n": a new line
  • "\r": a carriage return
  • "\0": NUL-byte
  • "\x0B": a vertical tab

    The character_list parameter lists characters to strip. character_list can be specified as a range by using the .. notation.

    Let's take a look at the examples below:

    Example 1

    print trim(' trim function is easy to use ');

    Result:

    trim function is easy to use

    The two spaces before the word 'trim' and the two spaces after the word 'use' are stripped out.

    Example 2

    print trim('trim function is easy to use', 't');

    Result:

    rim function is easy to use

    The character 't' at the beginning of the string is stripped out.

    Example 3

    print trim('trim function is easy to use', 'e,r..t');

    Result:

    im function is easy to u

    In this case, 'e', 'r', 's', 't' are all stripped if they are located at either end of the string. Therefore, 't', and 'r' are trimmed from the left, 's', and 'e' are trimmed from the right.

    Next: PHP Syntax

    Link to this page: If you find this page useful, we encourage you to link to this page. Simply copy and paste the code below to your website, blog, or profile.



    
    More 1Keydata Tutorials



  • PHP addslashes
    PHP echo
    PHP explode
    PHP htmlentities
    PHP htmlspecialchars
    PHP implode
    PHP md5
    PHP number_format
    PHP print
    PHP str_replace
    PHP strlen
    PHP strpos
    PHP strstr
    PHP substr
    PHP trim

    PHP Syntax

    PHP Sitemap

    PHP Resources