Broad Network


PHP Functions for Adding and Removing Slashes

PHP String Functions for Website Design – Part 1

Foreword: In this part of the series I talk about PHP Functions for adding and removing slashes for escaped sequences.

By: Chrysanthus Date Published: 19 Aug 2013

Introduction

This is part 1 of my series, PHP String Functions for Website Design. In this part of the series I talk about PHP Functions for adding and removing slashes for escaped sequences.

Note: the output of all the code samples of this tutorial series are sent to the browser.

Note: in this article, if you cannot see any text or piece of code or if you think something (e.g. an image) is missing or link does not operate, or just want to comment, contact me at forchatrans@yahoo.com .

Pre-Knowledge
There are certain topics you should have covered before reaching here, in order to understand this series. You can click the link, “PHP Course” below to see what you should have studied before reaching here.

The addslashes() Function
This function escapes the following characters in a string by preceding the character with the backslash:

    '  "  \  NUL

The syntax is:

    string addslashes ( string $str )


Read and try the following code:

<?php

    $str = "text ' text \ text \" text";
    $ret = addslashes($str);
    echo $ret;

?>


The output is:

    text \' text \\ text \" text

From the output you can see that ' has been escaped and \ has been escaped. Since " was already escaped in the string, the addslashes() function did not escape (add backslash) it again.

The stripslashes() Function
This function does the opposite of the above function. It removes the backslashes from the following escaped characters:

        '  "  \  NUL

The syntax is:

    string stripslashes ( string $str )

Read and try the following code:

<?php

    $str = "text \' text \\ text \" text";
    $ret = stripslashes($str);
    echo $ret;

?>

The output is:

    text ' text  text " text

\' was returned as '. As for \\, nothing (not the single slash) was returned. \" was returned as ".

The addcslashes() Function
For the addslashes() function, the characters that can be escaped are '  "  \  NUL . However, the addcslashes() function gives you the possibility to escape any character of your choice. The syntax is:

    string addcslashes ( string $str , string $charlist )

$str is the string with characters to be escaped. Here is what the specification has to say about the $charlist parameter: “A list of characters to be escaped. If charlist contains characters \n, \r etc., they are converted in C-like style, while other non-alphanumeric characters with ASCII codes lower than 32 and higher than 126 converted to octal representation.”

Read and try the following code:

<?php

    $str = "1, 2, and 3 are numbers";
    $ret = addcslashes ($str, "2rne");
    echo $ret;

?>

The output is:

    1, \2, a\nd 3 a\r\e \numb\e\rs

In the code the $charlist argument is, "2rne". So from the string, 2 was escaped, the two n’s were escaped, the two r’s were escaped, and the e was escaped.

The stripcslashes() Function
This function does the opposite of the addcslashes() function. The syntax is:

    string stripcslashes ( string $str )

Read and try the following code:

<?php

    $str = "1, \2, a\nd 3 a\r\e \numb\e\rs";
    $ret = stripcslashes($str);
    echo $ret;

?>

The output to the browser from my computer is:

    1,  , a d 3 a e umbe s

The character e came out as expected (unescaped), but \2, \n and \r did not come as expected. This is because, \2, \n, \r and other characters have special meaning in the function. You will consult some other document for that.

That is it for this part of the series. We stop here and continue in the next part.

Chrys

Related Links

Basics of PHP
PHP Directory Function Basics
Understanding PHP Reference
PHP Function Arguments
Understanding Variable Scope in PHP
Object Oriented Programming in PHP
PHP Data Types Simplified
Exception Handling in PHP
PHP Regular Expressions
Sending Email with PHP
PHP Strings
Date and Time in PHP
PHP String Functions for Website Design
Advanced Course
PHP Variable Scope
Advanced Features of PHP Function
Array in PHP
PHP Two Dimensional Arrays
Understanding Object Oriented Programming in PHP
Advanced PHP Regular Expressions
Some features of PHP Entities
PHP Namespace
PHP Web Application

More Related Links

Major in Website Design
Web Development Course
HTML Course
CSS Course
ECMAScript Course
PHP Course

NEXT

Comments

Become the Writer's Fan
Send the Writer a Message