---
title: "Some Important String function in PHP"  
description: "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."  
author: "Anonymous User"  
published: 2011-09-06  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/718/some-important-string-function-in-php  
category: "php"  
tags: ["php"]  
reading_time: 3 minutes  

---

# Some Important String function in PHP

There are lots of string [functions](https://www.mindstick.com/forum/34086/jquery-callback-functions) which are used in PHP. In this [article](https://yourviews.mindstick.com/view/81489/kashmir-now-after-an-year-of-abrogation-of-article-370) I will described some important [string function](https://www.mindstick.com/articles/719/php-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](https://answers.mindstick.com/qa/42112/who-is-the-originator-of-avengers-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](https://www.mindstick.com/mindstickarticle/9b7cff75-b9e4-44cb-a09b-15688e272d5a/images/9a4525cc-ffc1-4e6d-b21b-cf022a802ec4.png)

##### 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](https://www.mindstick.com/mindstickarticle/9b7cff75-b9e4-44cb-a09b-15688e272d5a/images/7b25bf3a-af82-45ed-bc84-d32983978768.png)

And the test.txt file is:

![Some Important String function in PHP](https://www.mindstick.com/mindstickarticle/9b7cff75-b9e4-44cb-a09b-15688e272d5a/images/0335ca0c-da03-4f46-9d58-7b491b336fb2.png)

##### 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](https://www.mindstick.com/mindstickarticle/9b7cff75-b9e4-44cb-a09b-15688e272d5a/images/f5b7e0c0-6ac2-4000-90a2-dc89e88692fa.png)

##### vprintf() function:

vprintf() function outputs the formatted string. It works as sprint, fprintf, but there is little bit [difference](https://www.mindstick.com/articles/157114/good-news-or-bad-news-and-the-difference-is) in syntax. Let’s see.

**Syntax:** vprintf ([format](https://www.mindstick.com/forum/162083/how-to-convert-ost-files-into-pst-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](https://www.mindstick.com/mindstickarticle/9b7cff75-b9e4-44cb-a09b-15688e272d5a/images/291d1f1a-7d0b-4db3-80b6-d5f4cf468963.png)

##### 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](https://www.mindstick.com/mindstickarticle/9b7cff75-b9e4-44cb-a09b-15688e272d5a/images/85523fb6-1df5-41a0-bdba-2ad9b4938a26.png)

[Form](https://yourviews.mindstick.com/view/87344/explained-the-benefits-of-short-form-videos) above example it is clear that you can [print](https://www.mindstick.com/blog/301752/types-of-3d-printing-technology) formatted string without using ‘echo’ statement.

\

---

Original Source: https://www.mindstick.com/articles/718/some-important-string-function-in-php

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
