PHP Tutorials

PHP String Functions

String functions in PHP

PHP String Functions

In this tutorial you will learn about some common string functions in PHP. This includes:

  1. Length of a string
  2. Splitting the string into an array
  3. Joining array elements into a single string
  4. Repeat the similar string into multiple times
  5. Remove the HTML tags from a string

1. Length of a string

The function 'strlen' is used to return the length of the string that is passed as an argument.

Syntax:

strlen(stringname);

Example:

$string="Baskar";

strlen($string); Output : 6

2. Splitting a string into an array

The function 'explode' is used to split a string into an array using a separator. It takes the separator and the string as its argument.

Syntax:

explode(separator,stringname);

Example:

$string="11-02-2005"

explode("-",$string); Output : 11 in array[0] , 02 in array[1] , 2005 in array[2]

3. Joining the array elements into a single string

The function 'join' is used to join various array elements into a single string. Alternatively 'implode' can also be used to join array elements into a string.

Syntax:

join(separator,arrayname);
(or)
implode(separator,arrayname);

Example:

$arraystring=array("s","baskar","shun");

implode("/",$arraystring); Output : s/baskar/shun

4. Repeating a string 'n' times

The function 'str_repeat' is used to repeat a given string 'n' times. The function takes the string and the number of times to be repeated as its argument.

Syntax:

str_repeat($string,integervalue);

Example:

str_repeat("dd",3); Output : dddddd

5. Remove HTML tags from the string

The function 'strip_tags' is used to remove the HTML tags present in a string by taking the string as its argument.

Syntax:

strip_tags($string);

Example:

strip_tags("Baskar s <br> Name",3); Output : Baskar S Name

That' it you've learnt about some common string functions used in PHP.

Please like, +1, link to and share this SmartWebby resource if you found it helpful. Thanks!