String Functions in PHP: Part 7

Introduction

In this article I describe the PHP string functions htmlspecialchars, implode and join. To learn some other math functions, go to:

  1. String Functions in PHP: Part1
     
  2. String Functions in PHP:Part2
     
  3. String Functions in PHP:Part3
     
  4. String Functions in PHP:Part4
     
  5. String Functions in PHP:Part5
     
  6. String Functions in PHP:Part6

PHP htmlspecialchars() Function

The PHP string htmlspecialchars function converts special characters to HTML entities and returns the converted string. It encodes user input on a website, so that the user cannot input harmful HTML codes into a site.

Syntax

htmlspecialchars (string, quotestyle,character-set)

Parameters in htmlspecialchars function

The parameters of the htmlspecialchars function are:

Parameter Description
string It specifies the string to convert.
quotestyle It specifies how to decode single or double quotes. And the quote styles are:
  • ENT_COMPAT - Default. Decodes only double quotes.
  • ENT_QUOTES - Decodes double and single quotes.
  • ENT_NOQUOTES - Does not decode any quotes.
  • ENT_HTML401 - Handle code as HTML 4.01.
  • ENT_XML1 - Handle code as XML1. 
character-set It specifies a string that specifies which character set is used.

Example

An example of the htmlspecialchars function is:


<?
php 
$
convert = "<li><a href='www.c-sharpcorner.com'>We are learning php</a></li>"
echo
"Original string : ".$convert
echo
"<br />"
htmlspecialchars
("<li><a href='www.c-sharpcorner.com'>We are learning php</a></li>", ENT_QUOTES); 
echo
"Only HTML special characters : ".htmlspecialchars($convert); 

?>

Output

htmlspecialchars-string-function-in-php.jpg


PHP implode() Function

The PHP string implode function converts an array into a string and returns a string representation of all the array elements in the same order.

Syntax

implode (separator, array)

Parameter in implode Function

The parameters of the implode function are:

Parameter Description
separator It specifies what to put between the array elements.
array It specifies the array to be joined into a string.

Example

An example of the
implode function is: 

<?
php
$
arr=array('MCN ','Solution ','at ','India!');
echo implode("",$arr);

?>

Output

implode-string-function-in-php.jpg
 

PHP join() Function

The PHP string join function is the same as the implode function, it is an alias for the implode function and is also used to add all elements of an array into a single string variable.

Syntax

join (separator, array)

Parameter in join function

The parameters of the join function are:

Parameter Description
separator It specifies what to put between the array elements.
array It specifies an array to join into a string.

Example

An example of the join
function is: 

<?php

$arr=array('MCN','Solution','at','India!');

echo join("-",$arr);

?>

Output

join-string-function-in-php.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all