articles

Home / DeveloperSection / Articles / Some Important String function in PHP

Some Important String function in PHP

Anonymous User 9382 06-Sep-2011

There are lots of string functions which are used in PHP. In this article I will described some important string function which is widely used in PHP. Let’s we have a brief description about it.

trim() function:

The trim() function removes whitespaces and other predefined characters from both sides of a string.

Syntax: trim (string name, charlist);

Example:
<!DOCTYPE html>
<html>
    <head>
       <title>Trim Demo</title>
   </head>
<body>
       <?php
              // Declare the string
              $lenDemo = "This is trim demo";
              // Declare string with special character
                $specStr = "This is trim demo with special character@@@@@";
              echo "<h5>Before Trim:</h5> ".   $lenDemo."</br>" ;
              echo "<h5>After Trim :</h5>".     trim($lenDemo)."</br>";
              echo "<h5>String with Special Character before trim</h5>".
$specStr."</br>";
               echo "<h5>String without Special Character after trim   :</h5>" .
trim($specStr, "@");
       ?>
</body>
</html>
Output:

Some Important String function in PHP

fprintf() function:

The fprintf() function writes a formatted string to a specified output stream (example: file or database).

The arg1, arg2 parameters will be inserted at percent (%) signs in the main string. This function works "step-by-step". At the first % sign, arg1 is inserted, at the second % sign, arg2 is inserted, etc.

Syntax: fprintf (stream, format, arg1, arg2,…)

Note: %s is used for print string value and %u is used for unsigned decimal value;

Example:
<html>
    <head>
       <title>Formatted String</title>
   </head>
<body>
       <?php
              $str1 = "Hello";
              $decimal = 123456;
              $str2 = 'room';
              $file = fopen("test.txt","w");
              echo fprintf($file,"%s Arun, ticket no. %u, your %s, %u",$str1,$decimal,$str2,$decimal);
       ?>
</body>
</html>
Output:

Some Important String function in PHP

And the test.txt file is:

Some Important String function in PHP

sprint() function :

The sprintf() function writes a formatted string to a variable.

The arg1, arg2, ++ parameters will be inserted at percent (%) signs in the main string. This function works "step-by-step". At the first % sign, arg1 is inserted, at the second % sign, arg2 is inserted, etc.

Syntax: sprint(format, arg1,arg2,…)

Example:
<html>
    <head>
       <title>Formatted String</title>
   </head>
<body>
       <?php
              $str1 = "Hello";
              $decimal = 123456;
              $str2 = 'room';
              $text = sprintf("%s Arun, ticket no. %u, your %s, %u",$str1,$decimal,$str2,$decimal);
              echo $text ;
       ?>
</body>
</html>
Output:

Some Important String function in PHP

vprintf() function:

vprintf() function outputs the formatted string. It works as sprint, fprintf, but there is little bit difference in syntax. Let’s see.

Syntax: vprintf (format, argarray)

Example 1(a):
<html>
    <head>
       <title>Formatted String</title>
   </head>
<body>
       <?php
              $str1 = "Hello";
              $decimal = 123456;
              $str2 = 'room';
              $text = vprintf("%s Arun, ticket no. %u, your %s,%u",array($str1,$decimal,$str2,$decimal));
              echo $text ;
       ?>
</body>
</html>
Output:

Some Important String function in PHP

Example 1(b):
<html>
    <head>
       <title>Formatted String</title>
   </head>
<body>
       <?php
              $str1 = "Hello";
              $decimal = 123456;
              $str2 = 'room';
               vprintf("%s Arun, ticket no. %u, your %s,%u",array($str1,$decimal,$str2,$decimal));
       ?>
</body>
</html>
Output:

Some Important String function in PHP

Form above example it is clear that you can print formatted string without using ‘echo’ statement.



php php 
Updated 07-Sep-2019
I am a content writter !

Leave Comment

Comments

Liked By